Skip to content

Commit c6dcdc9

Browse files
Migrate tests to JUnit5 (#2634)
Co-authored-by: strangelookingnerd <[email protected]>
1 parent 75cbbf1 commit c6dcdc9

File tree

51 files changed

+611
-639
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+611
-639
lines changed

integrations/src/test/java/io/jenkins/plugins/casc/CredentialsTest.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import static org.hamcrest.MatcherAssert.assertThat;
55
import static org.hamcrest.Matchers.is;
66
import static org.hamcrest.core.IsNot.not;
7-
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.assertNotNull;
9-
import static org.junit.Assert.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
import static org.junit.jupiter.api.Assertions.fail;
1011

1112
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
1213
import com.cloudbees.plugins.credentials.CredentialsProvider;
@@ -20,24 +21,22 @@
2021
import io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator;
2122
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
2223
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
24+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
2325
import io.jenkins.plugins.casc.model.CNode;
2426
import io.jenkins.plugins.casc.model.Mapping;
2527
import java.util.Collections;
2628
import java.util.List;
2729
import java.util.Set;
2830
import jenkins.model.Jenkins;
29-
import org.junit.Rule;
30-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
3132
import org.jvnet.hudson.test.Issue;
3233

33-
public class CredentialsTest {
34-
35-
@Rule
36-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
34+
@WithJenkinsConfiguredWithCode
35+
class CredentialsTest {
3736

3837
@ConfiguredWithCode("GlobalCredentials.yml")
3938
@Test
40-
public void testGlobalScopedCredentials() throws Exception {
39+
void testGlobalScopedCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
4140
List<StandardUsernamePasswordCredentials> creds = CredentialsProvider.lookupCredentials(
4241
StandardUsernamePasswordCredentials.class, Jenkins.getInstanceOrNull(), null, Collections.emptyList());
4342
assertThat(creds.size(), is(1));
@@ -60,7 +59,7 @@ public void testGlobalScopedCredentials() throws Exception {
6059

6160
@ConfiguredWithCode("CredentialsWithDomain.yml")
6261
@Test
63-
public void testDomainScopedCredentials() throws Exception {
62+
void testDomainScopedCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
6463
List<StandardUsernamePasswordCredentials> creds = CredentialsProvider.lookupCredentials(
6564
StandardUsernamePasswordCredentials.class, Jenkins.getInstanceOrNull(), null, Collections.emptyList());
6665
assertThat(creds.size(), is(1));
@@ -71,7 +70,7 @@ public void testDomainScopedCredentials() throws Exception {
7170

7271
@Test
7372
@ConfiguredWithCode("GlobalCredentials.yml")
74-
public void testExportFileCredentials() throws Exception {
73+
void testExportFileCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
7574
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
7675
ConfigurationContext context = new ConfigurationContext(registry);
7776
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
@@ -101,7 +100,7 @@ public void testExportFileCredentials() throws Exception {
101100

102101
@ConfiguredWithCode("GlobalCredentials.yml")
103102
@Test
104-
public void testExportSSHCredentials() throws Exception {
103+
void testExportSSHCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
105104
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
106105
ConfigurationContext context = new ConfigurationContext(registry);
107106
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
@@ -149,11 +148,11 @@ public void testExportSSHCredentials() throws Exception {
149148

150149
@Test
151150
@Issue("SECURITY-1404")
152-
public void checkUsernamePasswordIsSecret() throws Exception {
151+
void checkUsernamePasswordIsSecret(JenkinsConfiguredWithCodeRule j) throws Exception {
153152
Attribute a = getFromDatabound(UsernamePasswordCredentialsImpl.class, "password");
154153
assertTrue(
155-
"Attribute 'password' should be secret",
156-
a.isSecret(new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "1", "2", "3", "4")));
154+
a.isSecret(new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "1", "2", "3", "4")),
155+
"Attribute 'password' should be secret");
157156
}
158157

159158
@NonNull
@@ -165,6 +164,6 @@ public void checkUsernamePasswordIsSecret() throws Exception {
165164
return a;
166165
}
167166
}
168-
throw new AssertionError("Cannot find databound attribute " + attributeName + " in " + clazz);
167+
return fail("Cannot find databound attribute " + attributeName + " in " + clazz);
169168
}
170169
}

integrations/src/test/java/io/jenkins/plugins/casc/CustomToolsTest.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
package io.jenkins.plugins.casc;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
55

66
import com.cloudbees.jenkins.plugins.customtools.CustomTool;
77
import com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl;
88
import hudson.tools.CommandInstaller;
99
import hudson.tools.InstallSourceProperty;
1010
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
1111
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
12-
import org.junit.Ignore;
13-
import org.junit.Rule;
14-
import org.junit.Test;
12+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
13+
import org.junit.jupiter.api.Disabled;
14+
import org.junit.jupiter.api.Test;
1515
import org.jvnet.hudson.test.Issue;
1616

1717
/**
1818
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
1919
*/
20-
public class CustomToolsTest {
21-
22-
@Rule
23-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
20+
@WithJenkinsConfiguredWithCode
21+
class CustomToolsTest {
2422

2523
@Test
2624
@Issue("#97")
27-
@Ignore
25+
@Disabled
2826
@ConfiguredWithCode(value = "CustomToolsTest.yml")
29-
public void configure_custom_tools() {
27+
void configure_custom_tools(JenkinsConfiguredWithCodeRule j) {
3028
DescriptorImpl descriptor = (DescriptorImpl) j.jenkins.getDescriptorOrDie(CustomTool.class);
3129
assertEquals(1, descriptor.getInstallations().length);
3230
final CustomTool customTool = descriptor.getInstallations()[0];

integrations/src/test/java/io/jenkins/plugins/casc/DockerWorkflowSymbolTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@
55
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
66
import static org.hamcrest.MatcherAssert.assertThat;
77
import static org.hamcrest.core.Is.is;
8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1010

1111
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
1212
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
13+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
1314
import io.jenkins.plugins.casc.model.CNode;
1415
import org.jenkinsci.plugins.docker.workflow.declarative.GlobalConfig;
15-
import org.junit.ClassRule;
16-
import org.junit.Test;
16+
import org.junit.jupiter.api.Test;
1717
import org.jvnet.hudson.test.Issue;
1818

19-
public class DockerWorkflowSymbolTest {
20-
21-
@ClassRule
22-
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
23-
public static JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
19+
@WithJenkinsConfiguredWithCode
20+
class DockerWorkflowSymbolTest {
2421

2522
@Test
2623
@Issue("1260")
27-
public void configure_global_definition() {
24+
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
25+
void configure_global_definition(JenkinsConfiguredWithCodeRule j) {
2826
GlobalConfig config = GlobalConfig.get();
2927

3028
assertNotNull(config);
@@ -35,7 +33,8 @@ public void configure_global_definition() {
3533

3634
@Test
3735
@Issue("1260")
38-
public void export_global_definition() throws Exception {
36+
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
37+
void export_global_definition(JenkinsConfiguredWithCodeRule j) throws Exception {
3938
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
4039
ConfigurationContext context = new ConfigurationContext(registry);
4140
CNode yourAttribute = getUnclassifiedRoot(context).get("pipeline-model-docker");

integrations/src/test/java/io/jenkins/plugins/casc/EssentialsTest.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
import static org.hamcrest.CoreMatchers.is;
44
import static org.hamcrest.MatcherAssert.assertThat;
55
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
6-
import static org.junit.Assert.assertEquals;
7-
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNotNull;
88

99
import hudson.ExtensionList;
1010
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
1111
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
12+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
1213
import java.util.List;
1314
import jenkins.metrics.api.MetricsAccessKey;
1415
import jenkins.model.Jenkins;
15-
import org.junit.Rule;
16-
import org.junit.Test;
16+
import org.junit.jupiter.api.Test;
1717

18-
public class EssentialsTest {
19-
@Rule
20-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
18+
@WithJenkinsConfiguredWithCode
19+
class EssentialsTest {
2120

2221
@Test
2322
@ConfiguredWithCode("EssentialsTest.yml")
24-
public void essentialsTest() {
23+
void essentialsTest(JenkinsConfiguredWithCodeRule j) {
2524
final Jenkins jenkins = Jenkins.get();
2625
assertEquals("Welcome to Jenkins Essentials!", jenkins.getSystemMessage());
2726

integrations/src/test/java/io/jenkins/plugins/casc/GitTest.java

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
package io.jenkins.plugins.casc;
22

3-
import static org.junit.Assert.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import hudson.plugins.git.GitSCM;
66
import hudson.plugins.git.browser.AssemblaWeb;
77
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
88
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
9+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
910
import java.util.logging.Level;
1011
import java.util.logging.Logger;
1112
import jenkins.model.GlobalConfiguration;
1213
import jenkins.model.Jenkins;
1314
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
1415
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
1516
import org.jenkinsci.plugins.workflow.libs.SCMRetriever;
16-
import org.junit.After;
17-
import org.junit.Rule;
18-
import org.junit.Test;
19-
import org.junit.rules.RuleChain;
17+
import org.junit.jupiter.api.AfterEach;
18+
import org.junit.jupiter.api.Test;
2019
import org.jvnet.hudson.test.Issue;
21-
import org.jvnet.hudson.test.LoggerRule;
20+
import org.jvnet.hudson.test.LogRecorder;
2221

2322
/**
2423
* Tests for Git plugin global configurations.
2524
*/
26-
public class GitTest {
25+
@WithJenkinsConfiguredWithCode
26+
class GitTest {
2727

28-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
29-
public LoggerRule logging = new LoggerRule();
28+
private final LogRecorder logging = new LogRecorder()
29+
.record(Logger.getLogger(Attribute.class.getName()), Level.INFO)
30+
.capture(2048);
3031

31-
@Rule
32-
public RuleChain chain = RuleChain.outerRule(logging.record(Logger.getLogger(Attribute.class.getName()), Level.INFO)
33-
.capture(2048))
34-
.around(j);
35-
36-
@After
37-
public void dumpLogs() {
32+
@AfterEach
33+
void dumpLogs() {
3834
for (String message : logging.getMessages()) {
3935
System.out.println(message);
4036
}
@@ -43,7 +39,7 @@ public void dumpLogs() {
4339
@Test
4440
@Issue("JENKINS-57604")
4541
@ConfiguredWithCode("GitTest.yml")
46-
public void checkAssemblaWebIsLoaded() {
42+
void checkAssemblaWebIsLoaded(JenkinsConfiguredWithCodeRule j) {
4743
final Jenkins jenkins = Jenkins.get();
4844
final GlobalLibraries libs =
4945
jenkins.getExtensionList(GlobalConfiguration.class).get(GlobalLibraries.class);

integrations/src/test/java/io/jenkins/plugins/casc/GiteaOrganisationFolderTest.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package io.jenkins.plugins.casc;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
55

66
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
77
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
8+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
89
import java.util.List;
910
import jenkins.branch.OrganizationFolder;
1011
import jenkins.scm.api.trait.SCMTrait;
@@ -16,17 +17,14 @@
1617
import org.jenkinsci.plugin.gitea.SSHCheckoutTrait;
1718
import org.jenkinsci.plugin.gitea.TagDiscoveryTrait;
1819
import org.jenkinsci.plugin.gitea.WebhookRegistrationTrait;
19-
import org.junit.Rule;
20-
import org.junit.Test;
20+
import org.junit.jupiter.api.Test;
2121

22-
public class GiteaOrganisationFolderTest {
23-
24-
@Rule
25-
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
22+
@WithJenkinsConfiguredWithCode
23+
class GiteaOrganisationFolderTest {
2624

2725
@Test
2826
@ConfiguredWithCode("SeedJobTest_withGiteaOrganisation.yml")
29-
public void configure_gitea_organisation_folder_seed_job() {
27+
void configure_gitea_organisation_folder_seed_job(JenkinsConfiguredWithCodeRule r) {
3028
OrganizationFolder folder = (OrganizationFolder) r.jenkins.getItem("Gitea Organization Folder");
3129
assertNotNull(folder);
3230

integrations/src/test/java/io/jenkins/plugins/casc/GithubOrganisationFolderTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
package io.jenkins.plugins.casc;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
66

77
import hudson.model.TopLevelItem;
88
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
99
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
10+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
1011
import jenkins.branch.OrganizationFolder;
1112
import jenkins.model.Jenkins;
1213
import org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator;
13-
import org.junit.Rule;
14+
import org.junit.jupiter.api.Disabled;
15+
import org.junit.jupiter.api.Test;
1416

1517
/**
1618
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
1719
*/
18-
public class GithubOrganisationFolderTest {
20+
@WithJenkinsConfiguredWithCode
21+
class GithubOrganisationFolderTest {
1922

20-
@Rule
21-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
22-
23-
// @Test
24-
// Fails as Items do override submit() with manual data-binding implementation
23+
@Test
24+
@Disabled("Fails as Items do override submit() with manual data-binding implementation")
2525
@ConfiguredWithCode("GithubOrganisationFolderTest.yml")
26-
public void configure_github_organisation_folder_seed_job() {
26+
void configure_github_organisation_folder_seed_job(JenkinsConfiguredWithCodeRule j) {
2727
final TopLevelItem job = Jenkins.get().getItem("ndeloof");
2828
assertNotNull(job);
29-
assertTrue(job instanceof OrganizationFolder);
29+
assertInstanceOf(OrganizationFolder.class, job);
3030
OrganizationFolder folder = (OrganizationFolder) job;
3131
assertEquals(1, folder.getNavigators().size());
3232
final GitHubSCMNavigator github = folder.getNavigators().get(GitHubSCMNavigator.class);

integrations/src/test/java/io/jenkins/plugins/casc/GlobalLibrariesTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import static org.hamcrest.CoreMatchers.instanceOf;
44
import static org.hamcrest.MatcherAssert.assertThat;
5-
import static org.junit.Assert.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
66

77
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
88
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
9+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
910
import org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait;
1011
import org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait;
1112
import org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait.TrustPermission;
@@ -14,22 +15,19 @@
1415
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
1516
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
1617
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
17-
import org.junit.Rule;
18-
import org.junit.Test;
18+
import org.junit.jupiter.api.Test;
1919
import org.jvnet.hudson.test.Issue;
2020

2121
/**
2222
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
2323
*/
24-
public class GlobalLibrariesTest {
25-
26-
@Rule
27-
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
24+
@WithJenkinsConfiguredWithCode
25+
class GlobalLibrariesTest {
2826

2927
@Issue("JENKINS-57557")
3028
@Test
3129
@ConfiguredWithCode("GlobalLibrariesGitHubTest.yml")
32-
public void configure_global_library_using_github() {
30+
void configure_global_library_using_github(JenkinsConfiguredWithCodeRule j) {
3331
assertEquals(1, GlobalLibraries.get().getLibraries().size());
3432
final LibraryConfiguration library =
3533
GlobalLibraries.get().getLibraries().get(0);

0 commit comments

Comments
 (0)