diff --git a/.mvn/extensions.xml b/.mvn/extensions.xml index 9ac2968b..90787cbb 100644 --- a/.mvn/extensions.xml +++ b/.mvn/extensions.xml @@ -2,6 +2,6 @@ io.jenkins.tools.incrementals git-changelist-maven-extension - 1.4 + 1.6 diff --git a/github-pullrequest-plugin/pom.xml b/github-pullrequest-plugin/pom.xml index 858da8d0..4a6f5b3d 100644 --- a/github-pullrequest-plugin/pom.xml +++ b/github-pullrequest-plugin/pom.xml @@ -40,8 +40,6 @@ - 3.12.4 - 2.0.9 **/its/*.java true true @@ -65,8 +63,8 @@ io.jenkins.tools.bom - bom-2.346.x - 1763.v092b_8980a_f5e + bom-2.361.x + 2081.v85885a_d2e5c5 import pom @@ -241,27 +239,6 @@ test - - org.powermock - powermock-core - ${powermock.version} - test - - - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - - org.powermock - powermock-api-mockito2 - ${powermock.version} - test - - org.awaitility awaitility diff --git a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryFactoryTest.java b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryFactoryTest.java index bd6bd84a..67b4d2a5 100644 --- a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryFactoryTest.java +++ b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryFactoryTest.java @@ -4,7 +4,6 @@ import com.coravy.hudson.plugins.github.GithubProjectProperty; import com.coravy.hudson.plugins.github.GithubUrl; import hudson.XmlFile; -import hudson.model.AbstractItem; import hudson.model.AbstractProject; import hudson.model.Action; import hudson.model.ItemGroup; @@ -15,9 +14,8 @@ import org.junit.runner.RunWith; import org.kohsuke.github.GHRepository; import org.mockito.Mock; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.MockedStatic; +import org.mockito.junit.MockitoJUnitRunner; import edu.umd.cs.findbugs.annotations.CheckForNull; import java.io.File; @@ -28,29 +26,26 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; -import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.ghPRTriggerFromJob; import static org.junit.Assert.assertThat; -import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; /** * @author Alina_Karpovich */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({GithubProjectProperty.class, GithubUrl.class, JobHelper.class, AbstractItem.class, - GitHubPRTrigger.DescriptorImpl.class}) +@RunWith(MockitoJUnitRunner.class) public class GitHubPRRepositoryFactoryTest { public static final String CONFIG_PATH = "src/test/resources"; @Mock private GHRepository ghRepository; - @Mock + @Mock(lenient = true) private ItemGroup parent; @Mock private Job job; - @Mock + @Mock(lenient = true) private GitHubPRTrigger trigger; @Mock @@ -74,36 +69,37 @@ public void createForConfigFileIsBad() throws IOException, NoSuchFieldException, @Test public void createForNullTrigger() { // when(job.getTrigger(GitHubPRTrigger.class)).thenReturn(null); - PowerMockito.mockStatic(JobHelper.class); - given(ghPRTriggerFromJob(job)) - .willReturn(null); + try (MockedStatic mockedStatic = mockStatic(JobHelper.class)) { + mockedStatic.when(() -> JobHelper.ghPRTriggerFromJob(job)).thenReturn(null); Collection repoCollection = new GitHubPRRepositoryFactory().createFor(job); Assert.assertTrue(repoCollection instanceof List); Assert.assertTrue(repoCollection.isEmpty()); + } } @Test public void shouldNotCreateRepoForTriggerWithExc() throws Exception { // when(job.getTrigger(GitHubPRTrigger.class)).thenReturn(trigger); - PowerMockito.mockStatic(JobHelper.class); - given(ghPRTriggerFromJob(job)) - .willReturn(trigger); + try (MockedStatic mockedStatic = mockStatic(JobHelper.class)) { + mockedStatic.when(() -> JobHelper.ghPRTriggerFromJob(job)).thenReturn(trigger); when(parent.getFullName()).thenReturn("mocked job"); // when(job.getParent()).thenReturn(parent); when(trigger.getRepoFullName(job)).thenThrow(new RuntimeException()); assertThat(new GitHubPRRepositoryFactory().createFor(job), hasSize(0)); + } } private void createForCommonTest(String filePath) throws IOException, NoSuchFieldException, IllegalAccessException { - PowerMockito.mockStatic(GitHubPRTrigger.DescriptorImpl.class); - given(GitHubPRTrigger.DescriptorImpl.get()) - .willReturn(descriptor); + try (MockedStatic staticGitHubPRTriggerDescriptor = mockStatic(GitHubPRTrigger.DescriptorImpl.class); + MockedStatic staticJobHelper = mockStatic(JobHelper.class)) { + staticGitHubPRTriggerDescriptor.when(GitHubPRTrigger.DescriptorImpl::get).thenReturn(descriptor); + when(descriptor.isActualiseOnFactory()).thenReturn(false); - createForCommonExpectations(filePath, job, trigger); + createForCommonExpectations(filePath, job, trigger, staticJobHelper); when(trigger.getRemoteRepository()).thenReturn(ghRepository); @@ -113,6 +109,7 @@ private void createForCommonTest(String filePath) throws IOException, NoSuchFiel Assert.assertEquals(job, trigger.getJob()); Assert.assertEquals(new File(filePath), configFile.getFile().getParentFile()); + } } @CheckForNull @@ -130,8 +127,8 @@ public static GitHubPRRepository getRepo(Collection repoCollec * @param job mock job. * @param trigger mock trigger that is expected to be returned via job.getTrigger(GitHubPRTrigger.class). */ - public static void createForCommonExpectations(AbstractProject job, GitHubPRTrigger trigger) { - createForCommonExpectations(CONFIG_PATH, job, trigger); + public static void createForCommonExpectations(Job job, GitHubPRTrigger trigger, MockedStatic staticJobHelper) { + createForCommonExpectations(CONFIG_PATH, job, trigger, staticJobHelper); } /** @@ -143,18 +140,17 @@ public static void createForCommonExpectations(AbstractProject job, GitHub */ public static void createForCommonExpectations(String filePath, Job job, - GitHubPRTrigger trigger) { - GithubUrl githubUrl = PowerMockito.mock(GithubUrl.class); + GitHubPRTrigger trigger, + MockedStatic staticJobHelper) { + GithubUrl githubUrl = mock(GithubUrl.class); when(githubUrl.toString()).thenReturn("http://blaur"); - GithubProjectProperty projectProperty = PowerMockito.mock(GithubProjectProperty.class); + GithubProjectProperty projectProperty = mock(GithubProjectProperty.class); File file = new File(filePath); when(job.getRootDir()).thenReturn(file); when(job.getFullName()).thenReturn("jobFullName"); - PowerMockito.mockStatic(JobHelper.class); - given(ghPRTriggerFromJob(job)) - .willReturn(trigger); + staticJobHelper.when(() -> JobHelper.ghPRTriggerFromJob(job)).thenReturn(trigger); when(trigger.getJob()).thenReturn(job); when(trigger.getRepoFullName(job)).thenReturn(mock(GitHubRepositoryName.class)); diff --git a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryTest.java b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryTest.java index 9ed6bf87..61eb0b8a 100644 --- a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryTest.java +++ b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/GitHubPRRepositoryTest.java @@ -1,12 +1,10 @@ package org.jenkinsci.plugins.github.pullrequest; -import com.coravy.hudson.plugins.github.GithubProjectProperty; -import com.coravy.hudson.plugins.github.GithubUrl; import hudson.BulkChange; import hudson.Functions; -import hudson.model.AbstractProject; import hudson.model.Item; import hudson.model.ItemGroup; +import hudson.model.Job; import hudson.model.Result; import hudson.model.Run; import hudson.model.User; @@ -14,6 +12,7 @@ import hudson.util.FormValidation; import hudson.util.RunList; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.github.pullrequest.utils.JobHelper; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -21,13 +20,11 @@ import org.kohsuke.github.GHRepository; import org.kohsuke.stapler.StaplerRequest; import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.MockitoJUnitRunner; import org.mockito.stubbing.OngoingStubbing; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.List; @@ -38,9 +35,8 @@ import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.isNotNull; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -49,9 +45,7 @@ * Unit tests for GitHubPRRepository. */ @Ignore(value = "Mock issues") -@RunWith(PowerMockRunner.class) -@PrepareForTest({GithubProjectProperty.class, GithubUrl.class, BulkChange.class, - Functions.class, Jenkins.class, User.class, GHRepository.class}) +@RunWith(MockitoJUnitRunner.class) public class GitHubPRRepositoryTest { private static final int PR_REBUILD_ID = 1; @@ -61,7 +55,7 @@ public class GitHubPRRepositoryTest { @Mock private ItemGroup itemGroup; @Mock - private AbstractProject job; + private Job job; @Mock private GitHubPRTrigger trigger; @Mock @@ -87,7 +81,8 @@ public class GitHubPRRepositoryTest { @Test public void getAllPrBuildsWithCause() { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); @@ -97,11 +92,13 @@ public void getAllPrBuildsWithCause() { verify(iterator, times(BUILD_MAP_SIZE)).next(); Assert.assertEquals(0, prBuilds.size()); + } } @Test public void getAllPrBuildsNullCause() { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); when(run.getCause(GitHubPRCause.class)).thenReturn(null); @@ -110,63 +107,83 @@ public void getAllPrBuildsNullCause() { Map>> prBuilds = repo.getAllPrBuilds(); Assert.assertEquals(0, prBuilds.size()); + } } @Test public void doClearRepoPullsDeleted() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.DELETE, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class); + MockedStatic staticBulkChange = mockStatic(BulkChange.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.DELETE, true, mockedJenkins, mockedUser); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); - PowerMockito.mockStatic(BulkChange.class); - when(BulkChange.contains(repo)).thenReturn(true); + staticBulkChange.when(() -> BulkChange.contains(repo)).thenReturn(true); assertThat(repo.doClearRepo().kind, equalTo(FormValidation.Kind.OK)); assertThat(repo.getPulls().keySet(), hasSize(0)); + } } @Test public void doClearRepoWithException() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.DELETE, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class); + MockedStatic staticBulkChange = mockStatic(BulkChange.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.DELETE, true, mockedJenkins, mockedUser); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); - PowerMockito.mockStatic(BulkChange.class); - when(BulkChange.contains(repo)).thenThrow(new RuntimeException("bad save() for test")); + staticBulkChange.when(() -> BulkChange.contains(repo)).thenThrow(new RuntimeException("bad save() for test")); assertThat(repo.doClearRepo().kind, equalTo(FormValidation.Kind.ERROR)); assertThat(repo.getPulls().keySet(), hasSize(0)); + } } @Test public void doClearRepoForbidden() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.DELETE, false); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.DELETE, false, mockedJenkins, mockedUser); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); assertThat(repo.doClearRepo().kind, equalTo(FormValidation.Kind.ERROR)); assertThat(repo.getPulls().keySet(), hasSize(greaterThan(0))); + } } @Test public void doRebuildFailedNoRebuildNeeded() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.BUILD, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.BUILD, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); + } } @Test public void doRebuildFailedWithRebuildPerformed() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.BUILD, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.BUILD, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); @@ -178,12 +195,16 @@ public void doRebuildFailedWithRebuildPerformed() throws IOException { FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); + } } @Test public void doRebuildFailedWithException() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.BUILD, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.BUILD, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); @@ -193,25 +214,33 @@ public void doRebuildFailedWithException() throws IOException { FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); + } } @Test public void doRebuildFailedForbidden() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - hasPermissionExpectation(Item.BUILD, false); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + hasPermissionExpectation(Item.BUILD, false, mockedJenkins, mockedUser); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); + } } // FIXME: 1/7/16 @Ignore @Test public void doRebuildWithRebuildPerformed() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - doRebuildCommonExpectations(true, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + doRebuildCommonExpectations(true, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); @@ -223,12 +252,16 @@ public void doRebuildWithRebuildPerformed() throws IOException { FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); + } } @Test public void doRebuildWarnNotScheduled() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - doRebuildCommonExpectations(false, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + doRebuildCommonExpectations(false, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); @@ -238,12 +271,16 @@ public void doRebuildWarnNotScheduled() throws IOException { FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.WARNING, formValidation.kind); + } } @Test public void doRebuildWarnNotFound() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - doRebuildCommonExpectations(true, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + doRebuildCommonExpectations(true, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(0); @@ -253,12 +290,16 @@ public void doRebuildWarnNotFound() throws IOException { FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.WARNING, formValidation.kind); + } } @Test public void doRebuildWithException() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - doRebuildCommonExpectations(true, true); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + doRebuildCommonExpectations(true, true, mockedJenkins, mockedUser); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); @@ -268,28 +309,33 @@ public void doRebuildWithException() throws IOException { FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); + } } @Test public void doRebuildForbidden() throws IOException { - GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); - doRebuildCommonExpectations(true, false); + try (MockedStatic staticJobHelper = mockStatic(JobHelper.class); + MockedStatic mockedJenkins = mockStatic(Jenkins.class); + MockedStatic mockedUser = mockStatic(User.class)) { + GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger, staticJobHelper); + doRebuildCommonExpectations(true, false, mockedJenkins, mockedUser); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); + } } //to increase method coverage rate @Test public void checkGetters() throws IOException { + try (MockedStatic mockedFunctions = mockStatic(Functions.class)) { String fullName = "fullName"; URL url = new URL("https://github.com/user/repo/"); String prefix = "prefix"; - PowerMockito.mockStatic(Functions.class); - when(Functions.getResourcePath()).thenReturn(prefix); + mockedFunctions.when(Functions::getResourcePath).thenReturn(prefix); when(ghRepository.getFullName()).thenReturn("user/repo"); when(ghRepository.getHtmlUrl()).thenReturn(url); @@ -299,22 +345,21 @@ public void checkGetters() throws IOException { Assert.assertEquals(url, repo.getGithubUrl()); Assert.assertEquals("github-pullrequest", repo.getUrlName()); Assert.assertEquals(prefix + "/plugin/github-pullrequest/git-pull-request.svg", repo.getIconFileName()); + } } - private void doRebuildCommonExpectations(boolean hasParameter, boolean isAllowed) { - hasPermissionExpectation(Item.BUILD, isAllowed); + private void doRebuildCommonExpectations(boolean hasParameter, boolean isAllowed, MockedStatic mockedJenkins, MockedStatic mockedUser) { + hasPermissionExpectation(Item.BUILD, isAllowed, mockedJenkins, mockedUser); when(request.hasParameter(anyString())).thenReturn(hasParameter); if (hasParameter) { when(request.getParameter(anyString())).thenReturn(Integer.toString(PR_REBUILD_ID)); } } - private void hasPermissionExpectation(Permission permission, boolean isAllowed) { - PowerMockito.mockStatic(Jenkins.class); - when(Jenkins.getInstance()).thenReturn(instance); + private void hasPermissionExpectation(Permission permission, boolean isAllowed, MockedStatic mockedJenkins, MockedStatic mockedUser) { + mockedJenkins.when(Jenkins::getInstance).thenReturn(instance); when(instance.hasPermission(permission)).thenReturn(isAllowed); - PowerMockito.mockStatic(User.class); - when(User.current()).thenReturn(user); + mockedUser.when(User::current).thenReturn(user); } private void getAllPrBuildsCommonExpectations(int size) { diff --git a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/events/impl/GitHubPRDescriptionEventTest.java b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/events/impl/GitHubPRDescriptionEventTest.java index a8619060..d325f8dd 100644 --- a/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/events/impl/GitHubPRDescriptionEventTest.java +++ b/github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/events/impl/GitHubPRDescriptionEventTest.java @@ -1,7 +1,7 @@ package org.jenkinsci.plugins.github.pullrequest.events.impl; import hudson.model.FreeStyleProject; -import hudson.model.Job; +import hudson.model.ItemGroup; import hudson.model.TaskListener; import org.jenkinsci.plugins.github.pullrequest.GitHubPRCause; import org.jenkinsci.plugins.github.pullrequest.GitHubPRTrigger; @@ -14,12 +14,10 @@ import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHUser; import org.mockito.Mock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import java.io.IOException; import java.io.PrintStream; -import java.util.Date; import static com.github.kostyasha.github.integration.generic.GitHubPRDecisionContext.newGitHubPRDecisionContext; import static org.hamcrest.Matchers.nullValue; @@ -33,16 +31,15 @@ /** * @author Kanstantsin Shautsou */ -@PrepareForTest(Job.class) -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class GitHubPRDescriptionEventTest { @Mock - private FreeStyleProject job; + private ItemGroup parent; @Mock private GHPullRequest remotePr; - @Mock + @Mock(lenient = true) private GHRepository repository; @Mock private GHIssue issue; @@ -62,7 +59,6 @@ public void skipDescriptionExist() throws IOException { when(listener.getLogger()).thenReturn(logger); - when(issue.getCreatedAt()).thenReturn(new Date()); when(remotePr.getBody()).thenReturn("must skip ci body"); GitHubPRCause cause = new GitHubPRDescriptionEvent(".*[skip ci].*") @@ -84,7 +80,6 @@ public void skipDescriptionNotExist() throws IOException { when(listener.getLogger()).thenReturn(logger); - when(issue.getCreatedAt()).thenReturn(new Date()); when(remotePr.getBody()).thenReturn("unmatched comment"); GitHubPRCause cause = new GitHubPRDescriptionEvent(".*skip ci.*") @@ -99,9 +94,10 @@ public void skipDescriptionNotExist() throws IOException { } private void commonExpectations() throws IOException { - when(job.getFullName()).thenReturn("Full job name"); + when(parent.getFullName()).thenReturn("Full job name"); - when(trigger.getJob()).thenReturn((Job) job); + FreeStyleProject p = new FreeStyleProject(parent, "p"); + when(trigger.getJob()).thenReturn(p); when(remotePr.getState()).thenReturn(GHIssueState.OPEN); when(remotePr.getRepository()).thenReturn(repository); diff --git a/pom.xml b/pom.xml index 63f1ec17..9dfa68c4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.jenkins-ci.plugins plugin - 4.43.1 + 4.63 @@ -34,7 +34,7 @@ 0.5.1 -SNAPSHOT - 2.346.3 + 2.361.4