Skip to content

Migrate tests to JUnit5 #2634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsProvider;
Expand All @@ -20,24 +21,22 @@
import io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

public class CredentialsTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class CredentialsTest {

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

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

@Test
@ConfiguredWithCode("GlobalCredentials.yml")
public void testExportFileCredentials() throws Exception {
void testExportFileCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
Expand Down Expand Up @@ -101,7 +100,7 @@ public void testExportFileCredentials() throws Exception {

@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() throws Exception {
void testExportSSHCredentials(JenkinsConfiguredWithCodeRule j) throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
Expand Down Expand Up @@ -149,11 +148,11 @@ public void testExportSSHCredentials() throws Exception {

@Test
@Issue("SECURITY-1404")
public void checkUsernamePasswordIsSecret() throws Exception {
void checkUsernamePasswordIsSecret(JenkinsConfiguredWithCodeRule j) throws Exception {
Attribute a = getFromDatabound(UsernamePasswordCredentialsImpl.class, "password");
assertTrue(
"Attribute 'password' should be secret",
a.isSecret(new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "1", "2", "3", "4")));
a.isSecret(new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "1", "2", "3", "4")),
"Attribute 'password' should be secret");
}

@NonNull
Expand All @@ -165,6 +164,6 @@ public void checkUsernamePasswordIsSecret() throws Exception {
return a;
}
}
throw new AssertionError("Cannot find databound attribute " + attributeName + " in " + clazz);
return fail("Cannot find databound attribute " + attributeName + " in " + clazz);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package io.jenkins.plugins.casc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.cloudbees.jenkins.plugins.customtools.CustomTool;
import com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl;
import hudson.tools.CommandInstaller;
import hudson.tools.InstallSourceProperty;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public class CustomToolsTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class CustomToolsTest {

@Test
@Issue("#97")
@Ignore
@Disabled
@ConfiguredWithCode(value = "CustomToolsTest.yml")
public void configure_custom_tools() {
void configure_custom_tools(JenkinsConfiguredWithCodeRule j) {
DescriptorImpl descriptor = (DescriptorImpl) j.jenkins.getDescriptorOrDie(CustomTool.class);
assertEquals(1, descriptor.getInstallations().length);
final CustomTool customTool = descriptor.getInstallations()[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import io.jenkins.plugins.casc.model.CNode;
import org.jenkinsci.plugins.docker.workflow.declarative.GlobalConfig;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

public class DockerWorkflowSymbolTest {

@ClassRule
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
public static JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class DockerWorkflowSymbolTest {

@Test
@Issue("1260")
public void configure_global_definition() {
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
void configure_global_definition(JenkinsConfiguredWithCodeRule j) {
GlobalConfig config = GlobalConfig.get();

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

@Test
@Issue("1260")
public void export_global_definition() throws Exception {
@ConfiguredWithCode("DockerWorkflowSymbol.yml")
void export_global_definition(JenkinsConfiguredWithCodeRule j) throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CNode yourAttribute = getUnclassifiedRoot(context).get("pipeline-model-docker");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import hudson.ExtensionList;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import java.util.List;
import jenkins.metrics.api.MetricsAccessKey;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class EssentialsTest {
@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class EssentialsTest {

@Test
@ConfiguredWithCode("EssentialsTest.yml")
public void essentialsTest() {
void essentialsTest(JenkinsConfiguredWithCodeRule j) {
final Jenkins jenkins = Jenkins.get();
assertEquals("Welcome to Jenkins Essentials!", jenkins.getSystemMessage());

Expand Down
30 changes: 13 additions & 17 deletions integrations/src/test/java/io/jenkins/plugins/casc/GitTest.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package io.jenkins.plugins.casc;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import hudson.plugins.git.GitSCM;
import hudson.plugins.git.browser.AssemblaWeb;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import org.jenkinsci.plugins.workflow.libs.SCMRetriever;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.LogRecorder;

/**
* Tests for Git plugin global configurations.
*/
public class GitTest {
@WithJenkinsConfiguredWithCode
class GitTest {

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

@Rule
public RuleChain chain = RuleChain.outerRule(logging.record(Logger.getLogger(Attribute.class.getName()), Level.INFO)
.capture(2048))
.around(j);

@After
public void dumpLogs() {
@AfterEach
void dumpLogs() {
for (String message : logging.getMessages()) {
System.out.println(message);
}
Expand All @@ -43,7 +39,7 @@ public void dumpLogs() {
@Test
@Issue("JENKINS-57604")
@ConfiguredWithCode("GitTest.yml")
public void checkAssemblaWebIsLoaded() {
void checkAssemblaWebIsLoaded(JenkinsConfiguredWithCodeRule j) {
final Jenkins jenkins = Jenkins.get();
final GlobalLibraries libs =
jenkins.getExtensionList(GlobalConfiguration.class).get(GlobalLibraries.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.jenkins.plugins.casc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import java.util.List;
import jenkins.branch.OrganizationFolder;
import jenkins.scm.api.trait.SCMTrait;
Expand All @@ -16,17 +17,14 @@
import org.jenkinsci.plugin.gitea.SSHCheckoutTrait;
import org.jenkinsci.plugin.gitea.TagDiscoveryTrait;
import org.jenkinsci.plugin.gitea.WebhookRegistrationTrait;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class GiteaOrganisationFolderTest {

@Rule
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class GiteaOrganisationFolderTest {

@Test
@ConfiguredWithCode("SeedJobTest_withGiteaOrganisation.yml")
public void configure_gitea_organisation_folder_seed_job() {
void configure_gitea_organisation_folder_seed_job(JenkinsConfiguredWithCodeRule r) {
OrganizationFolder folder = (OrganizationFolder) r.jenkins.getItem("Gitea Organization Folder");
assertNotNull(folder);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package io.jenkins.plugins.casc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import hudson.model.TopLevelItem;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import jenkins.branch.OrganizationFolder;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator;
import org.junit.Rule;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public class GithubOrganisationFolderTest {
@WithJenkinsConfiguredWithCode
class GithubOrganisationFolderTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();

// @Test
// Fails as Items do override submit() with manual data-binding implementation
@Test
@Disabled("Fails as Items do override submit() with manual data-binding implementation")
@ConfiguredWithCode("GithubOrganisationFolderTest.yml")
public void configure_github_organisation_folder_seed_job() {
void configure_github_organisation_folder_seed_job(JenkinsConfiguredWithCodeRule j) {
final TopLevelItem job = Jenkins.get().getItem("ndeloof");
assertNotNull(job);
assertTrue(job instanceof OrganizationFolder);
assertInstanceOf(OrganizationFolder.class, job);
OrganizationFolder folder = (OrganizationFolder) job;
assertEquals(1, folder.getNavigators().size());
final GitHubSCMNavigator github = folder.getNavigators().get(GitHubSCMNavigator.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait;
import org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait;
import org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait.TrustPermission;
Expand All @@ -14,22 +15,19 @@
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public class GlobalLibrariesTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class GlobalLibrariesTest {

@Issue("JENKINS-57557")
@Test
@ConfiguredWithCode("GlobalLibrariesGitHubTest.yml")
public void configure_global_library_using_github() {
void configure_global_library_using_github(JenkinsConfiguredWithCodeRule j) {
assertEquals(1, GlobalLibraries.get().getLibraries().size());
final LibraryConfiguration library =
GlobalLibraries.get().getLibraries().get(0);
Expand Down
Loading
Loading