Skip to content

Commit

Permalink
Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
* Migrate annotations and imports
* Migrate assertions
* Migrate GHMock Rule to Extension
* Migrate to DataProviderRunner to ParameterizedTest
* Remove public visibility for test classes and methods
* Minor clean up
  • Loading branch information
strangelookingnerd committed Jan 23, 2025
1 parent a50f733 commit f0f7dbe
Show file tree
Hide file tree
Showing 53 changed files with 1,035 additions and 1,080 deletions.
20 changes: 9 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
Expand Down Expand Up @@ -135,6 +135,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<!-- For GlobalMatrixAuthorizationStrategy -->
<dependency>
Expand Down Expand Up @@ -172,18 +177,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tngtech.java</groupId>
<artifactId>junit-dataprovider</artifactId>
<version>1.13.1</version>
<scope>test</scope>
</dependency>

<!--to mock github-->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.35.2</version>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>

Expand Down
67 changes: 32 additions & 35 deletions src/test/java/com/cloudbees/jenkins/GitHubCommitNotifierTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cloudbees.jenkins;

import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Build;
Expand All @@ -13,24 +13,22 @@
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import hudson.util.VersionNumber;
import jakarta.inject.Inject;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.jenkinsci.plugins.github.test.GHMockRule;
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.jenkinsci.plugins.github.test.GitHubMockExtension;
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import jakarta.inject.Inject;
import org.mockito.junit.jupiter.MockitoExtension;

import static com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest.SOME_SHA;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
Expand All @@ -45,43 +43,42 @@
*
* @author <a href="mailto:o.v.nenashev@gmail.com">Oleg Nenashev</a>
*/
@RunWith(MockitoJUnitRunner.class)
@WithJenkins
@ExtendWith(MockitoExtension.class)
public class GitHubCommitNotifierTest {

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
public BuildData data;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
public Revision rev;

@Inject
public GitHubPluginConfig config;

public JenkinsRule jRule = new JenkinsRule();
private JenkinsRule jRule;

@Rule
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));

@Rule
public GHMockRule github = new GHMockRule(
new WireMockRule(
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
))
@RegisterExtension
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))))
.stubUser()
.stubRepo()
.stubStatuses();


@Before
public void before() throws Throwable {
when(data.getLastBuiltRevision()).thenReturn(rev);
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
@BeforeEach
void before(JenkinsRule rule) throws Throwable {
jRule = rule;
jRule.getInstance().getInjector().injectMembers(this);

when(data.getLastBuiltRevision()).thenReturn(rev);
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
}

@Test
@Issue("JENKINS-23641")
public void testNoBuildData() throws Exception {
void testNoBuildData() throws Exception {
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData");
prj.getPublishersList().add(new GitHubCommitNotifier());
Build b = prj.scheduleBuild2(0).get();
Expand All @@ -91,7 +88,7 @@ public void testNoBuildData() throws Exception {

@Test
@Issue("JENKINS-23641")
public void testNoBuildRevision() throws Exception {
void testNoBuildRevision() throws Exception {
FreeStyleProject prj = jRule.createFreeStyleProject();
prj.setScm(new GitSCM("http://non.existent.git.repo.nowhere/repo.git"));
prj.getPublishersList().add(new GitHubCommitNotifier());
Expand All @@ -103,7 +100,7 @@ public void testNoBuildRevision() throws Exception {

@Test
@Issue("JENKINS-25312")
public void testMarkUnstableOnCommitNotifierFailure() throws Exception {
void testMarkUnstableOnCommitNotifierFailure() throws Exception {
FreeStyleProject prj = jRule.createFreeStyleProject();
prj.getPublishersList().add(new GitHubCommitNotifier(Result.UNSTABLE.toString()));
Build b = prj.scheduleBuild2(0).get();
Expand All @@ -112,15 +109,15 @@ public void testMarkUnstableOnCommitNotifierFailure() throws Exception {

@Test
@Issue("JENKINS-25312")
public void testMarkSuccessOnCommitNotifierFailure() throws Exception {
void testMarkSuccessOnCommitNotifierFailure() throws Exception {
FreeStyleProject prj = jRule.createFreeStyleProject();
prj.getPublishersList().add(new GitHubCommitNotifier(Result.SUCCESS.toString()));
Build b = prj.scheduleBuild2(0).get();
jRule.assertBuildStatus(Result.SUCCESS, b);
}

@Test
public void shouldWriteStatusOnGH() throws Exception {
void shouldWriteStatusOnGH() throws Exception {
config.getConfigs().add(github.serverConfig());
FreeStyleProject prj = jRule.createFreeStyleProject();

Expand All @@ -136,7 +133,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

prj.scheduleBuild2(0).get();

github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
}

private Build safelyGenerateBuild(FreeStyleProject prj) throws InterruptedException, java.util.concurrent.ExecutionException {
Expand Down
25 changes: 13 additions & 12 deletions src/test/java/com/cloudbees/jenkins/GitHubPushTriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;
import hudson.util.FormValidation;
import jakarta.inject.Inject;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.admin.GitHubHookRegisterProblemMonitor;
import org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventListenerTest;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
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.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import jakarta.inject.Inject;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -30,7 +30,8 @@
/**
* @author lanwen (Merkushev Kirill)
*/
public class GitHubPushTriggerTest {
@WithJenkins
class GitHubPushTriggerTest {
private static final GitHubRepositoryName REPO = new GitHubRepositoryName("host", "user", "repo");
private static final GitSCM REPO_GIT_SCM = new GitSCM("git://host/user/repo.git");

Expand All @@ -40,11 +41,11 @@ public class GitHubPushTriggerTest {
@Inject
private GitHubPushTrigger.DescriptorImpl descriptor;

@Rule
public JenkinsRule jRule = new JenkinsRule();
private JenkinsRule jRule;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp(JenkinsRule rule) throws Exception {
jRule = rule;
jRule.getInstance().getInjector().injectMembers(this);
}

Expand All @@ -53,7 +54,7 @@ public void setUp() throws Exception {
*/
@Test
@Issue("JENKINS-27136")
public void shouldStartWorkflowByTrigger() throws Exception {
void shouldStartWorkflowByTrigger() throws Exception {
WorkflowJob job = jRule.getInstance().createProject(WorkflowJob.class, "test-workflow-job");
GitHubPushTrigger trigger = new GitHubPushTrigger();
trigger.start(job, false);
Expand All @@ -79,7 +80,7 @@ public void shouldStartWorkflowByTrigger() throws Exception {

@Test
@Issue("JENKINS-24690")
public void shouldReturnWaringOnHookProblem() throws Exception {
void shouldReturnWaringOnHookProblem() throws Exception {
monitor.registerProblem(REPO, new IOException());
FreeStyleProject job = jRule.createFreeStyleProject();
job.setScm(REPO_GIT_SCM);
Expand All @@ -89,7 +90,7 @@ public void shouldReturnWaringOnHookProblem() throws Exception {
}

@Test
public void shouldReturnOkOnNoAnyProblem() throws Exception {
void shouldReturnOkOnNoAnyProblem() throws Exception {
FreeStyleProject job = jRule.createFreeStyleProject();
job.setScm(REPO_GIT_SCM);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cloudbees.jenkins;

import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Build;
Expand All @@ -11,26 +11,25 @@
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import hudson.tasks.Builder;
import jakarta.inject.Inject;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.jenkinsci.plugins.github.test.GHMockRule;
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.jenkinsci.plugins.github.test.GitHubMockExtension;
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.hudson.test.recipes.LocalData;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import jakarta.inject.Inject;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
Expand All @@ -44,44 +43,43 @@
*
* @author <a href="mailto:o.v.nenashev@gmail.com">Oleg Nenashev</a>
*/
@RunWith(MockitoJUnitRunner.class)
@WithJenkins
@ExtendWith(MockitoExtension.class)
public class GitHubSetCommitStatusBuilderTest {

public static final String SOME_SHA = StringUtils.repeat("f", 40);

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
public BuildData data;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
public Revision rev;

@Inject
public GitHubPluginConfig config;

public JenkinsRule jRule = new JenkinsRule();
private JenkinsRule jRule;

@Rule
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));

@Rule
public GHMockRule github = new GHMockRule(
new WireMockRule(
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
))
@RegisterExtension
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))))
.stubUser()
.stubRepo()
.stubStatuses();

@Before
public void before() throws Throwable {
when(data.getLastBuiltRevision()).thenReturn(rev);
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
@BeforeEach
void before(JenkinsRule rule) throws Throwable {
jRule = rule;
jRule.getInstance().getInjector().injectMembers(this);

when(data.getLastBuiltRevision()).thenReturn(rev);
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
}

@Test
@Issue("JENKINS-23641")
public void shouldIgnoreIfNoBuildData() throws Exception {
void shouldIgnoreIfNoBuildData() throws Exception {
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData");
prj.getBuildersList().add(new GitHubSetCommitStatusBuilder());
Build b = prj.scheduleBuild2(0).get();
Expand All @@ -91,7 +89,7 @@ public void shouldIgnoreIfNoBuildData() throws Exception {
@Test
@LocalData
@Issue("JENKINS-32132")
public void shouldLoadNullStatusMessage() throws Exception {
void shouldLoadNullStatusMessage() throws Exception {
config.getConfigs().add(github.serverConfig());
FreeStyleProject prj = jRule.getInstance().getItemByFullName("step", FreeStyleProject.class);

Expand All @@ -107,7 +105,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
prj.getBuildersList().replaceBy(builders);
prj.scheduleBuild2(0).get();

github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
}

@TestExtension
Expand Down
Loading

0 comments on commit f0f7dbe

Please sign in to comment.