Skip to content
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

Migrate tests to JUnit5 #387

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 10 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.2</version>
<version>5.6</version>
<relativePath />
</parent>

Expand Down 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:[email protected]">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
Loading
Loading