Skip to content

Commit b38f161

Browse files
Migrate tests to JUnit5 (#142)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 58f9917 commit b38f161

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
3838
<!--Enabling static analysis using spotbugs checks-->
3939
<spotbugs.effort>Max</spotbugs.effort>
40-
<spotbugs.threshold>Low</spotbugs.threshold> <!--Decreased threshold for more deeper checks-->
40+
<spotbugs.threshold>Low</spotbugs.threshold> <!--Decreased threshold for more deeper checks-->
4141
</properties>
4242

4343
<scm>
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
package org.jenkinsci.plugins.buildnamesetter;
22

3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.io.IOException;
6-
import java.util.concurrent.ExecutionException;
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
75

86
import hudson.EnvVars;
97
import hudson.model.FreeStyleBuild;
108
import hudson.model.FreeStyleProject;
119
import hudson.model.Result;
1210
import hudson.model.TaskListener;
1311
import org.jenkinsci.plugins.EnvironmentVarSetter;
14-
import org.junit.Assert;
15-
import org.junit.Rule;
16-
import org.junit.Test;
12+
import org.junit.jupiter.api.Test;
1713
import org.jvnet.hudson.test.Issue;
1814
import org.jvnet.hudson.test.JenkinsRule;
15+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1916

20-
public class BuildNameSetterTest {
21-
22-
@Rule
23-
public JenkinsRule jenkins = new JenkinsRule();
17+
@WithJenkins
18+
class BuildNameSetterTest {
2419

2520
@Test
26-
public void shouldExpand_BUILD_NUMBER_macro() throws Exception {
21+
void shouldExpand_BUILD_NUMBER_macro(JenkinsRule jenkins) throws Exception {
2722
FreeStyleProject fooProj = jenkins.createFreeStyleProject("foo");
2823
fooProj.getBuildWrappersList().add(getDefaultSetter("a_#${BUILD_NUMBER}"));
2924

@@ -32,7 +27,7 @@ public void shouldExpand_BUILD_NUMBER_macro() throws Exception {
3227
}
3328

3429
@Test
35-
public void shouldExpand_JOB_NAME_full_env_macro() throws InterruptedException, ExecutionException, IOException {
30+
void shouldExpand_JOB_NAME_full_env_macro(JenkinsRule jenkins) throws Exception {
3631
FreeStyleProject barProj = jenkins.createFreeStyleProject("bar");
3732
barProj.getBuildWrappersList().add(getDefaultSetter("b_${ENV,var=\"JOB_NAME\"}"));
3833

@@ -42,7 +37,7 @@ public void shouldExpand_JOB_NAME_full_env_macro() throws InterruptedException,
4237

4338
@Issue("13347")
4439
@Test
45-
public void shouldExpand_JOB_NAME_macro() throws InterruptedException, ExecutionException, IOException {
40+
void shouldExpand_JOB_NAME_macro(JenkinsRule jenkins) throws Exception {
4641
FreeStyleProject barProj = jenkins.createFreeStyleProject("bar");
4742
barProj.getBuildWrappersList().add(getDefaultSetter("c_${JOB_NAME}"));
4843

@@ -52,7 +47,7 @@ public void shouldExpand_JOB_NAME_macro() throws InterruptedException, Execution
5247

5348
@Issue("13347")
5449
@Test
55-
public void shouldExpand_JOB_NAME_macro_twice() throws InterruptedException, ExecutionException, IOException {
50+
void shouldExpand_JOB_NAME_macro_twice(JenkinsRule jenkins) throws Exception {
5651
FreeStyleProject barProj = jenkins.createFreeStyleProject("bar");
5752
barProj.getBuildWrappersList().add(getDefaultSetter("c_${JOB_NAME}_d_${JOB_NAME}"));
5853

@@ -62,7 +57,7 @@ public void shouldExpand_JOB_NAME_macro_twice() throws InterruptedException, Exe
6257

6358
@Issue("13347")
6459
@Test
65-
public void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro() throws InterruptedException, ExecutionException, IOException {
60+
void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro(JenkinsRule jenkins) throws Exception {
6661
FreeStyleProject fooProj = jenkins.createFreeStyleProject("foo");
6762
fooProj.getBuildWrappersList().add(getDefaultSetter("d_${NODE_NAME}_${ENV,var=\"JOB_NAME\"}"));
6863

@@ -72,30 +67,26 @@ public void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro() throws In
7267

7368
@Issue("34181")
7469
@Test
75-
public void shouldUse_default_config_values_if_null() throws InterruptedException, ExecutionException, IOException {
70+
void shouldUse_default_config_values_if_null(JenkinsRule jenkins) throws Exception {
7671
FreeStyleProject fooProj = jenkins.createFreeStyleProject("foo");
7772
fooProj.getBuildWrappersList().add(new BuildNameSetter("${ENV,var=\"JOB_NAME\"}", null, null));
7873

7974
FreeStyleBuild fooBuild = fooProj.scheduleBuild2(0).get();
8075
assertDisplayName(fooBuild, "foo");
8176
}
8277

83-
private void assertDisplayName(FreeStyleBuild build, String expectedName) {
78+
private static void assertDisplayName(FreeStyleBuild build, String expectedName) {
8479
assertEquals(Result.SUCCESS, build.getResult());
8580
assertEquals(expectedName, build.getDisplayName());
8681
EnvironmentVarSetter action = build.getAction(EnvironmentVarSetter.class);
8782
assertEquals(expectedName, action.getVar(EnvironmentVarSetter.buildDisplayNameVar));
88-
EnvVars envVars = null;
89-
try {
90-
envVars = build.getEnvironment(TaskListener.NULL);
91-
} catch (Exception e) {
92-
e.printStackTrace();
93-
Assert.fail("Exception was thrown during getting build environment:" + e.getMessage());
94-
}
83+
EnvVars envVars = assertDoesNotThrow(
84+
() -> build.getEnvironment(TaskListener.NULL),
85+
"Exception was thrown during getting build environment");
9586
assertEquals(expectedName, envVars.get(EnvironmentVarSetter.buildDisplayNameVar));
9687
}
9788

98-
private BuildNameSetter getDefaultSetter(String template) {
89+
private static BuildNameSetter getDefaultSetter(String template) {
9990
return new BuildNameSetter(template, true, true);
10091
}
10192
}

0 commit comments

Comments
 (0)