Skip to content

Commit

Permalink
Use JUnit Jupiter (#122)
Browse files Browse the repository at this point in the history
* refactor: JUnit Jupiter migration from JUnit 4.x

Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.testing.junit5.JUnit4to5Migration

Co-authored-by: Moderne <[email protected]>

* Add mockito-junit-jupiter dependency

* Use WithJenkins for JenkinsRule on JUnit 5

Exclude BuildTimeoutWrapperIntegrationTest because LocalData annotation
does not work with JenkinsRule on JUnit 5

---------

Co-authored-by: Moderne <[email protected]>
Co-authored-by: Steve Hill <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2023
1 parent 51c03b7 commit 1e9c5d8
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 180 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@
import hudson.plugins.build_timeout.operations.AbortOperation;
import hudson.plugins.build_timeout.operations.FailOperation;
import hudson.tasks.Builder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BuildStepWithTimeoutTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@WithJenkins
class BuildStepWithTimeoutTest {

private final static long TINY_DELAY = 100L;
private final static long HUGE_DELAY = 5000L;

@Before
@BeforeEach
public void before() {
// this allows timeout shorter than 3 minutes.
BuildTimeoutWrapper.MINIMUM_TIMEOUT_MILLISECONDS = 100;
}

@Test
public void timeoutWasNotTriggered() throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(TINY_DELAY, null);
void timeoutWasNotTriggered(JenkinsRule j) throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(TINY_DELAY, null, j);

final FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserIdCause()).get();

Expand All @@ -41,8 +40,8 @@ public void timeoutWasNotTriggered() throws Exception {
}

@Test
public void timeoutWasTriggeredWithoutAction() throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, null);
void timeoutWasTriggeredWithoutAction(JenkinsRule j) throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, null, j);

final FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserIdCause()).get();

Expand All @@ -51,8 +50,8 @@ public void timeoutWasTriggeredWithoutAction() throws Exception {
}

@Test
public void timeoutWasTriggeredWithAbortOperation() throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, new AbortOperation());
void timeoutWasTriggeredWithAbortOperation(JenkinsRule j) throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, new AbortOperation(), j);

final FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserIdCause()).get();

Expand All @@ -61,16 +60,16 @@ public void timeoutWasTriggeredWithAbortOperation() throws Exception {
}

@Test
public void timeoutWasTriggeredWithFailOperation() throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, new FailOperation());
void timeoutWasTriggeredWithFailOperation(JenkinsRule j) throws Exception {
final FreeStyleProject project = createProjectWithBuildStepWithTimeout(HUGE_DELAY, new FailOperation(), j);

final FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserIdCause()).get();

j.assertBuildStatus(Result.FAILURE, build);
j.assertLogNotContains(FakeBuildStep.FAKE_BUILD_STEP_OUTPUT, build);
}

private FreeStyleProject createProjectWithBuildStepWithTimeout(long delay, BuildTimeOutOperation operation) throws IOException {
private FreeStyleProject createProjectWithBuildStepWithTimeout(long delay, BuildTimeOutOperation operation, JenkinsRule j) throws IOException {
final FreeStyleProject project = j.createFreeStyleProject();
final List<BuildTimeOutOperation> operations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.time.Duration;
Expand All @@ -22,9 +21,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

@ExtendWith(MockitoExtension.class)
public class GlobalTimeOutRunListenerTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock
private TimeOutProvider timeOutProvider;
@Mock
Expand All @@ -38,7 +36,7 @@ public class GlobalTimeOutRunListenerTest {
@Mock
private BuildListener buildListener;

@Before
@BeforeEach
public void setup() {
listener = new GlobalTimeOutRunListener(
Executors.newSingleThreadScheduledExecutor(),
Expand All @@ -48,7 +46,7 @@ public void setup() {
}

@Test
public void shouldStoreIfPresent() throws IOException, InterruptedException {
void shouldStoreIfPresent() throws IOException, InterruptedException {
given(timeOutProvider.timeOutFor(build, buildListener)).willReturn(Optional.of(Duration.ofMillis(1)));
given(build.getExternalizableId()).willReturn("a#1");

Expand All @@ -58,7 +56,7 @@ public void shouldStoreIfPresent() throws IOException, InterruptedException {
}

@Test
public void shouldNotStoreIfAbsent() throws IOException, InterruptedException {
void shouldNotStoreIfAbsent() throws IOException, InterruptedException {
given(timeOutProvider.timeOutFor(build, buildListener)).willReturn(Optional.empty());

listener.setUpEnvironment(build, launcher, buildListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hudson.plugins.build_timeout.global;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
Expand All @@ -17,15 +17,15 @@ public class InMemoryTimeOutStoreTest {
private final TimeOutStore store = new InMemoryTimeOutStore(map);

@Test
public void shouldKeep() {
void shouldKeep() {
store.scheduled("a", mock(ScheduledFuture.class));

assertEquals(1, map.size());
assertTrue(map.containsKey("a"));
}

@Test
public void shouldNoOpIfKeyAlreadyExists() {
void shouldNoOpIfKeyAlreadyExists() {
store.scheduled("a", mock(ScheduledFuture.class));
assertEquals(1, map.size());

Expand All @@ -35,7 +35,7 @@ public void shouldNoOpIfKeyAlreadyExists() {
}

@Test
public void shouldRemoveKeyFromMap() {
void shouldRemoveKeyFromMap() {
store.scheduled("a", mock(ScheduledFuture.class));
store.scheduled("b", mock(ScheduledFuture.class));

Expand All @@ -46,7 +46,7 @@ public void shouldRemoveKeyFromMap() {
}

@Test
public void shouldCancel() {
void shouldCancel() {
ScheduledFuture<?> a = mock(ScheduledFuture.class);
ScheduledFuture<?> b = mock(ScheduledFuture.class);
store.scheduled("a", a);
Expand All @@ -59,7 +59,7 @@ public void shouldCancel() {
}

@Test
public void shouldNoOpIfAbsent() {
void shouldNoOpIfAbsent() {
store.scheduled("a", mock(ScheduledFuture.class));
assertEquals(1, map.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.plugins.build_timeout.BuildTimeOutOperation;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import org.mockito.junit.jupiter.MockitoExtension;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Duration;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
public class TimeOutTaskTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock
private TimeOutProvider timeOutProvider;
@Mock
Expand All @@ -31,13 +28,13 @@ public class TimeOutTaskTest {
private BuildListener listener;
private TimeOutTask task;

@Before
@BeforeEach
public void setup() {
task = TimeOutTask.create(timeOutProvider, build, listener, Duration.ofMillis(1));
}

@Test
public void shouldCallAllOperations() {
void shouldCallAllOperations() {
TestOp one = new TestOp();
TestOp two = new TestOp();
given(timeOutProvider.getOperations()).willReturn(Lists.newArrayList(one, two));
Expand All @@ -49,7 +46,7 @@ public void shouldCallAllOperations() {
}

@Test
public void shouldStopAtFirstFailure() {
void shouldStopAtFirstFailure() {
TestOp one = new TestOp();
FailsOp two = new FailsOp();
TestOp three = new TestOp();
Expand All @@ -63,7 +60,7 @@ public void shouldStopAtFirstFailure() {
}

@Test
public void shouldStopAtFirstException() {
void shouldStopAtFirstException() {
TestOp one = new TestOp();
ThrowsOp two = new ThrowsOp();
TestOp three = new TestOp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,37 @@
import hudson.plugins.build_timeout.BuildTimeoutWrapperIntegrationTest;
import hudson.plugins.build_timeout.operations.AbortOperation;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/**
* Tests for {@link AbsoluteTimeOutStrategy}.
* Many tests for {@link AbsoluteTimeOutStrategy} are also in {@link BuildTimeoutWrapperIntegrationTest}
*/
public class AbsoluteTimeOutStrategyTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@WithJenkins
class AbsoluteTimeOutStrategyTest {

private long origTimeout = 0;

@Before
public void before() {
@BeforeEach
void before() {
// this allows timeout shorter than 3 minutes.
origTimeout = BuildTimeoutWrapper.MINIMUM_TIMEOUT_MILLISECONDS;
BuildTimeoutWrapper.MINIMUM_TIMEOUT_MILLISECONDS = 1000;
}

@After
public void after() {
@AfterEach
void after() {
BuildTimeoutWrapper.MINIMUM_TIMEOUT_MILLISECONDS = origTimeout;
}

@Test
public void configurationWithParameter() throws Exception {
void configurationWithParameter(JenkinsRule j) throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
// needed since Jenkins 2.3
p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("TIMEOUT", null)));
Expand Down
Loading

0 comments on commit 1e9c5d8

Please sign in to comment.