From 5b0f366efb082de321ce3e8b7d9fb3b16e532c6c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 23 Sep 2021 11:03:53 +0000 Subject: [PATCH 1/2] Bump plugin from 4.25 to 4.26 Bumps [plugin](https://github.com/jenkinsci/plugin-pom) from 4.25 to 4.26. - [Release notes](https://github.com/jenkinsci/plugin-pom/releases) - [Changelog](https://github.com/jenkinsci/plugin-pom/blob/master/CHANGELOG.md) - [Commits](https://github.com/jenkinsci/plugin-pom/compare/plugin-4.25...plugin-4.26) --- updated-dependencies: - dependency-name: org.jenkins-ci.plugins:plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a10fac75d..f0b3f84d7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.jenkins-ci.plugins plugin - 4.25 + 4.26 From e49836c698039a161780ee60b70ecc3a6e7cfcbc Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sun, 26 Sep 2021 14:26:10 -0700 Subject: [PATCH 2/2] Remove PowerMock --- pom.xml | 12 +- .../content/FailedTestsContentTest.java | 16 +- .../ScriptContentBuildWrapperTest.java | 20 -- .../CulpritsRecipientProviderTest.java | 95 +++++---- .../DevelopersRecipientProviderTest.java | 72 ++++--- ...lingTestSuspectsRecipientProviderTest.java | 180 +++++++++--------- ...ingBuildSuspectsRecipientProviderTest.java | 176 ++++++++--------- .../plugins/recipients/MockUtilities.java | 35 ++-- .../RecipientProviderUtilitiesTest.java | 14 +- 9 files changed, 268 insertions(+), 352 deletions(-) diff --git a/pom.xml b/pom.xml index f0b3f84d7..7369d4b4e 100644 --- a/pom.xml +++ b/pom.xml @@ -237,17 +237,7 @@ org.mockito - mockito-core - test - - - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito2 + mockito-inline test diff --git a/src/test/java/hudson/plugins/emailext/plugins/content/FailedTestsContentTest.java b/src/test/java/hudson/plugins/emailext/plugins/content/FailedTestsContentTest.java index 16d9a109c..55712687e 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/content/FailedTestsContentTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/content/FailedTestsContentTest.java @@ -8,28 +8,18 @@ import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import static java.util.Collections.singletonList; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - -@SuppressWarnings({"unchecked"}) -@RunWith(PowerMockRunner.class) -@PrepareForTest( { TestResult.class }) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class FailedTestsContentTest { private FailedTestsContent failedTestContent; @@ -257,7 +247,7 @@ public void testGetContent_withMessage_withStack_htmlEscaped() throws Exception when(result.getErrorDetails()).thenReturn("expected: but was: "); when(result.getErrorStackTrace()).thenReturn("at org.nexusformat.NexusFile.(NexusFile.java:99)"); - Mockito.>when(testResults.getFailedTests()).thenReturn(singletonList(result)); + Mockito.>when(testResults.getFailedTests()).thenReturn(Collections.singletonList(result)); when(build.getAction(AbstractTestResultAction.class)).thenReturn(testResults); failedTestContent.showMessage = true; @@ -347,7 +337,7 @@ public void testGetContent_withMessage_withStack_outputYaml() throws Exception { when(result.getErrorDetails()).thenReturn("expected: but was: "); when(result.getErrorStackTrace()).thenReturn(testStackTrace); - Mockito.>when(testResults.getFailedTests()).thenReturn(singletonList(result)); + Mockito.>when(testResults.getFailedTests()).thenReturn(Collections.singletonList(result)); when(build.getAction(AbstractTestResultAction.class)).thenReturn(testResults); failedTestContent.showMessage = true; diff --git a/src/test/java/hudson/plugins/emailext/plugins/content/ScriptContentBuildWrapperTest.java b/src/test/java/hudson/plugins/emailext/plugins/content/ScriptContentBuildWrapperTest.java index 7c81014fa..27e4e9e39 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/content/ScriptContentBuildWrapperTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/content/ScriptContentBuildWrapperTest.java @@ -2,7 +2,6 @@ import hudson.Functions; import hudson.model.AbstractBuild; -import hudson.model.Action; import hudson.tasks.junit.TestResult; import hudson.tasks.junit.TestResultAction; import hudson.tasks.test.AbstractTestResultAction; @@ -10,10 +9,6 @@ import hudson.tasks.test.AggregatedTestResultAction.ChildReport; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import java.util.Calendar; import java.util.LinkedList; @@ -26,9 +21,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@RunWith( PowerMockRunner.class ) -@PrepareForTest( value = { AggregatedTestResultAction.class, AggregatedTestResultAction.ChildReport.class } ) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class ScriptContentBuildWrapperTest { private ScriptContentBuildWrapper buildWrapper; @@ -60,18 +52,6 @@ public void testGetAction_whenActionNotFoundThenReturnNull() assertNull( buildWrapper.getAction( "class.not.found" ) ); } - @Test - public void testGetAction_returnActionWhenFoundByName() - { - final Action expectedAction = mock( Action.class ); - when( mockBuild.getActions() ).thenReturn( new LinkedList() - {{ - add( expectedAction ); - }} ); - - assertSame( expectedAction, buildWrapper.getAction( expectedAction.getClass().getName() ) ); - } - @Test public void testGetJUnitTestResult_whenMavenProjectUseMavenPluginsSurefireAggregatedReport() { diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java index 99d601f8d..62ce42a88 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java @@ -1,93 +1,84 @@ package hudson.plugins.emailext.plugins.recipients; -import hudson.model.FreeStyleBuild; -import hudson.model.Job; import hudson.model.Result; -import hudson.model.User; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; import jenkins.model.Jenkins; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.MockedStatic; +import org.mockito.Mockito; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - ExtendedEmailPublisherDescriptor.class, - FreeStyleBuild.class, - Jenkins.class, - Mailer.class, - Mailer.DescriptorImpl.class, - User.class, - WorkflowRun.class, - WorkflowJob.class, - Job.class -}) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class CulpritsRecipientProviderTest { + private MockedStatic mockedJenkins; + private MockedStatic mockedMailer; + @Before public void before() throws Exception { - final Jenkins jenkins = PowerMockito.mock(Jenkins.class); - PowerMockito.when(jenkins.isUseSecurity()).thenReturn(false); - final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = PowerMockito.mock(ExtendedEmailPublisherDescriptor.class); + final Jenkins jenkins = Mockito.mock(Jenkins.class); + Mockito.when(jenkins.isUseSecurity()).thenReturn(false); + final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = Mockito.mock(ExtendedEmailPublisherDescriptor.class); extendedEmailPublisherDescriptor.setDebugMode(true); - PowerMockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); + Mockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); + + Mockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); + mockedJenkins = Mockito.mockStatic(Jenkins.class); + mockedJenkins.when(Jenkins::get).thenReturn(jenkins); - PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); - PowerMockito.mockStatic(Jenkins.class); - PowerMockito.doReturn(jenkins).when(Jenkins.class, "get"); + final Mailer.DescriptorImpl descriptor = Mockito.mock(Mailer.DescriptorImpl.class); + Mockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); + mockedMailer = Mockito.mockStatic(Mailer.class); + mockedMailer.when(Mailer::descriptor).thenReturn(descriptor); + } - final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); - PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); - PowerMockito.mockStatic(Mailer.class); - PowerMockito.doReturn(descriptor).when(Mailer.class, "descriptor"); + @After + public void after() { + mockedMailer.close(); + mockedJenkins.close(); } @Test public void testAddRecipients1() throws Exception { - final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); - final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); + final WorkflowJob j = Mockito.mock(WorkflowJob.class); + final WorkflowRun build1 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build1, "X", "V"); - PowerMockito.doReturn(null).when(build1).getPreviousBuild(); + Mockito.doReturn(null).when(build1).getPreviousBuild(); - final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS); + final WorkflowRun build2 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build2.getResult()).thenReturn(Result.SUCCESS); MockUtilities.addChangeSet(build2, "Z", "V"); - PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); + Mockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); - final WorkflowRun build3 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build3.getResult()).thenReturn(Result.UNSTABLE); + final WorkflowRun build3 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build3.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build3, "A"); - PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); + Mockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); - final WorkflowRun build4 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build4.getResult()).thenReturn(Result.UNSTABLE); + final WorkflowRun build4 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build4.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build4, "B"); - PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); + Mockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); TestUtilities.checkRecipients(build4, new CulpritsRecipientProvider(), "A", "B"); } @Test public void testAddRecipients2() throws Exception { - final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); - final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); + final WorkflowJob j = Mockito.mock(WorkflowJob.class); + final WorkflowRun build1 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build1, "X", "V"); - PowerMockito.doReturn(null).when(build1).getPreviousBuild(); + Mockito.doReturn(null).when(build1).getPreviousBuild(); - final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS); + final WorkflowRun build2 = Mockito.spy(new WorkflowRun(j)); + Mockito.when(build2.getResult()).thenReturn(Result.SUCCESS); MockUtilities.addChangeSet(build2, "Z", "V"); - PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); + Mockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); TestUtilities.checkRecipients(build2, new CulpritsRecipientProvider(), "X", "V", "Z"); } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java index 6a2594528..ce9109a35 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java @@ -9,59 +9,55 @@ import jenkins.model.Jenkins; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.MockedStatic; +import org.mockito.Mockito; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - ExtendedEmailPublisherDescriptor.class, - FreeStyleBuild.class, - Jenkins.class, - Mailer.class, - Mailer.DescriptorImpl.class, - User.class, - WorkflowRun.class, - WorkflowJob.class, - FreeStyleProject.class -}) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class DevelopersRecipientProviderTest { + private MockedStatic mockedJenkins; + private MockedStatic mockedMailer; + @Before public void before() throws Exception { - final Jenkins jenkins = PowerMockito.mock(Jenkins.class); - PowerMockito.when(jenkins.isUseSecurity()).thenReturn(false); - final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = PowerMockito.mock(ExtendedEmailPublisherDescriptor.class); + final Jenkins jenkins = Mockito.mock(Jenkins.class); + Mockito.when(jenkins.isUseSecurity()).thenReturn(false); + final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = Mockito.mock(ExtendedEmailPublisherDescriptor.class); extendedEmailPublisherDescriptor.setDebugMode(true); - PowerMockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); + Mockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); + + Mockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); + mockedJenkins = Mockito.mockStatic(Jenkins.class); + mockedJenkins.when(Jenkins::get).thenReturn(jenkins); - PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); - PowerMockito.mockStatic(Jenkins.class); - PowerMockito.doReturn(jenkins).when(Jenkins.class, "get"); + final Mailer.DescriptorImpl descriptor = Mockito.mock(Mailer.DescriptorImpl.class); + Mockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); + mockedMailer = Mockito.mockStatic(Mailer.class); + mockedMailer.when(Mailer::descriptor).thenReturn(descriptor); + } - final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); - PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); - PowerMockito.mockStatic(Mailer.class); - PowerMockito.doReturn(descriptor).when(Mailer.class, "descriptor"); + @After + public void after() { + mockedMailer.close(); + mockedJenkins.close(); } @Test public void testAddRecipients() throws Exception { - final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); - final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build1).getResult(); - MockUtilities.addRequestor(build1, "A"); - MockUtilities.addChangeSet(build1, "X", "V"); - TestUtilities.checkRecipients(build1, new DevelopersRecipientProvider(), "X", "V"); + try (MockedStatic mockedUser = Mockito.mockStatic(User.class)) { + final FreeStyleProject p = Mockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build1).getResult(); + MockUtilities.addRequestor(mockedUser, build1, "A"); + MockUtilities.addChangeSet(build1, "X", "V"); + TestUtilities.checkRecipients(build1, new DevelopersRecipientProvider(), "X", "V"); + } - final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); - final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); - PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); + final WorkflowJob j = Mockito.mock(WorkflowJob.class); + final WorkflowRun build2 = Mockito.spy(new WorkflowRun(j)); + Mockito.doReturn(Result.UNSTABLE).when(build2).getResult(); MockUtilities.addChangeSet(build2, "X", "V"); TestUtilities.checkRecipients(build2, new DevelopersRecipientProvider(), "X", "V"); } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java index 4b138cf91..3649aca7b 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java @@ -23,7 +23,6 @@ */ package hudson.plugins.emailext.plugins.recipients; -import hudson.PluginManager; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.Result; @@ -31,51 +30,40 @@ import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; import jenkins.model.Jenkins; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.MockedStatic; +import org.mockito.Mockito; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - ExtendedEmailPublisherDescriptor.class, - FreeStyleBuild.class, - Jenkins.class, - Mailer.class, - Mailer.DescriptorImpl.class, - User.class, - FreeStyleProject.class, - PluginManager.class -}) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class FailingTestSuspectsRecipientProviderTest { + private MockedStatic mockedJenkins; + private MockedStatic mockedMailer; + @Before public void before() throws Exception { - final Jenkins jenkins = PowerMockito.mock(Jenkins.class); - PowerMockito.when(jenkins.isUseSecurity()).thenReturn(false); - final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = PowerMockito.mock(ExtendedEmailPublisherDescriptor.class); + final Jenkins jenkins = Mockito.mock(Jenkins.class); + Mockito.when(jenkins.isUseSecurity()).thenReturn(false); + final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = Mockito.mock(ExtendedEmailPublisherDescriptor.class); extendedEmailPublisherDescriptor.setDebugMode(true); - PowerMockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); - - PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); + Mockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); - final PluginManager pluginManager = PowerMockito.mock(PluginManager.class); - Whitebox.setInternalState(pluginManager, "uberClassLoader", this.getClass().getClassLoader()); + Mockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); - PowerMockito.when(jenkins.getPluginManager()).thenReturn(pluginManager); + mockedJenkins = Mockito.mockStatic(Jenkins.class); + mockedJenkins.when(Jenkins::get).thenReturn(jenkins); - PowerMockito.mockStatic(Jenkins.class); - PowerMockito.doReturn(jenkins).when(Jenkins.class, "get"); + final Mailer.DescriptorImpl descriptor = Mockito.mock(Mailer.DescriptorImpl.class); + Mockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); + mockedMailer = Mockito.mockStatic(Mailer.class); + mockedMailer.when(Mailer::descriptor).thenReturn(descriptor); + } - final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); - PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); - PowerMockito.mockStatic(Mailer.class); - PowerMockito.doReturn(descriptor).when(Mailer.class, "descriptor"); + @After + public void after() { + mockedMailer.close(); + mockedJenkins.close(); } @Test @@ -86,71 +74,73 @@ public void testAddRecipients() throws Exception { * No committers. * Tests {a,b} fail. */ - final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); - final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build1).getResult(); - PowerMockito.doReturn(null).when(build1).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build1, "A"); - MockUtilities.addTestResultAction(build1, build1, build1); - TestUtilities.checkRecipients(build1, new FailingTestSuspectsRecipientProvider(), "A"); + try (MockedStatic mockedUser = Mockito.mockStatic(User.class)) { + final FreeStyleProject p = Mockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build1).getResult(); + Mockito.doReturn(null).when(build1).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build1, "A"); + MockUtilities.addTestResultAction(build1, build1, build1); + TestUtilities.checkRecipients(build1, new FailingTestSuspectsRecipientProvider(), "A"); - /* - * Requestor: (none) - * Committers {U,V}. - * Tests {a,b,c} fail. - */ - final FreeStyleBuild build2 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); - PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); - MockUtilities.addChangeSet(build2, "U", "V"); - MockUtilities.addTestResultAction(build2, build1, build1, build2); - TestUtilities.checkRecipients(build2, new FailingTestSuspectsRecipientProvider(), "A", "U", "V"); + /* + * Requestor: (none) + * Committers {U,V}. + * Tests {a,b,c} fail. + */ + final FreeStyleBuild build2 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build2).getResult(); + Mockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); + MockUtilities.addChangeSet(build2, "U", "V"); + MockUtilities.addTestResultAction(build2, build1, build1, build2); + TestUtilities.checkRecipients(build2, new FailingTestSuspectsRecipientProvider(), "A", "U", "V"); - /* - * Requestor: (none) - * Committers {X,V}. - * Tests {c,d} fail. - */ - final FreeStyleBuild build3 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build3).getResult(); - PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); - MockUtilities.addChangeSet(build3, "X", "V"); - MockUtilities.addTestResultAction(build3, build2, build3); - TestUtilities.checkRecipients(build3, new FailingTestSuspectsRecipientProvider(), "U", "V", "X"); + /* + * Requestor: (none) + * Committers {X,V}. + * Tests {c,d} fail. + */ + final FreeStyleBuild build3 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build3).getResult(); + Mockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); + MockUtilities.addChangeSet(build3, "X", "V"); + MockUtilities.addTestResultAction(build3, build2, build3); + TestUtilities.checkRecipients(build3, new FailingTestSuspectsRecipientProvider(), "U", "V", "X"); - /* - * Requestor: (none) - * Committers {K} - * No tests were performed. The build failed. - */ - final FreeStyleBuild build4 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.FAILURE).when(build4).getResult(); - PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); - MockUtilities.addChangeSet(build4, "K"); - TestUtilities.checkRecipients(build4, new FailingTestSuspectsRecipientProvider()); + /* + * Requestor: (none) + * Committers {K} + * No tests were performed. The build failed. + */ + final FreeStyleBuild build4 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.FAILURE).when(build4).getResult(); + Mockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); + MockUtilities.addChangeSet(build4, "K"); + TestUtilities.checkRecipients(build4, new FailingTestSuspectsRecipientProvider()); - /* - * Requestor: (none) - * Committers {X,U,V}. - * No tests were performed. The build failed. - */ - final FreeStyleBuild build5 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.FAILURE).when(build5).getResult(); - PowerMockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); - MockUtilities.addChangeSet(build5, "U", "W"); - TestUtilities.checkRecipients(build5, new FailingTestSuspectsRecipientProvider()); + /* + * Requestor: (none) + * Committers {X,U,V}. + * No tests were performed. The build failed. + */ + final FreeStyleBuild build5 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.FAILURE).when(build5).getResult(); + Mockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); + MockUtilities.addChangeSet(build5, "U", "W"); + TestUtilities.checkRecipients(build5, new FailingTestSuspectsRecipientProvider()); - /* - * Requestor: A - * Committers {W}. - * Tests {a,e (new test)} fail. - */ - final FreeStyleBuild build6 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build6).getResult(); - PowerMockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build6, "A"); - MockUtilities.addChangeSet(build6, "W"); - MockUtilities.addTestResultAction(build6, build6, build6); - TestUtilities.checkRecipients(build6, new FailingTestSuspectsRecipientProvider(), "A", "K", "U", "W"); + /* + * Requestor: A + * Committers {W}. + * Tests {a,e (new test)} fail. + */ + final FreeStyleBuild build6 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build6).getResult(); + Mockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build6, "A"); + MockUtilities.addChangeSet(build6, "W"); + MockUtilities.addTestResultAction(build6, build6, build6); + TestUtilities.checkRecipients(build6, new FailingTestSuspectsRecipientProvider(), "A", "K", "U", "W"); + } } } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java index 89964b60b..c4e35aa7a 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java @@ -23,7 +23,6 @@ */ package hudson.plugins.emailext.plugins.recipients; -import hudson.PluginManager; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.Result; @@ -31,50 +30,39 @@ import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; import jenkins.model.Jenkins; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.MockedStatic; +import org.mockito.Mockito; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - ExtendedEmailPublisherDescriptor.class, - FreeStyleBuild.class, - Jenkins.class, - Mailer.class, - Mailer.DescriptorImpl.class, - User.class, - FreeStyleProject.class, - PluginManager.class -}) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class FirstFailingBuildSuspectsRecipientProviderTest { + private MockedStatic mockedJenkins; + private MockedStatic mockedMailer; + @Before public void before() throws Exception { - final Jenkins jenkins = PowerMockito.mock(Jenkins.class); - PowerMockito.when(jenkins.isUseSecurity()).thenReturn(false); - final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = PowerMockito.mock(ExtendedEmailPublisherDescriptor.class); + final Jenkins jenkins = Mockito.mock(Jenkins.class); + Mockito.when(jenkins.isUseSecurity()).thenReturn(false); + final ExtendedEmailPublisherDescriptor extendedEmailPublisherDescriptor = Mockito.mock(ExtendedEmailPublisherDescriptor.class); extendedEmailPublisherDescriptor.setDebugMode(true); - PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); - - final PluginManager pluginManager = PowerMockito.mock(PluginManager.class); - Whitebox.setInternalState(pluginManager, "uberClassLoader", this.getClass().getClassLoader()); + Mockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); - PowerMockito.when(jenkins.getPluginManager()).thenReturn(pluginManager); + mockedJenkins = Mockito.mockStatic(Jenkins.class); + mockedJenkins.when(Jenkins::get).thenReturn(jenkins); - PowerMockito.mockStatic(Jenkins.class); - PowerMockito.doReturn(jenkins).when(Jenkins.class, "get"); + final Mailer.DescriptorImpl descriptor = Mockito.mock(Mailer.DescriptorImpl.class); + Mockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); + mockedMailer = Mockito.mockStatic(Mailer.class); + mockedMailer.when(Mailer::descriptor).thenReturn(descriptor); + } - final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); - PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); - PowerMockito.mockStatic(Mailer.class); - PowerMockito.doReturn(descriptor).when(Mailer.class, "descriptor"); + @After + public void after() { + mockedMailer.close(); + mockedJenkins.close(); } @Test @@ -85,70 +73,72 @@ public void testAddRecipients() throws Exception { * No committers. * Failed. */ - final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); - final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.FAILURE).when(build1).getResult(); - PowerMockito.doReturn(null).when(build1).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build1, "A"); - TestUtilities.checkRecipients(build1, new FirstFailingBuildSuspectsRecipientProvider(), "A"); + try (MockedStatic mockedUser = Mockito.mockStatic(User.class)) { + final FreeStyleProject p = Mockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.FAILURE).when(build1).getResult(); + Mockito.doReturn(null).when(build1).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build1, "A"); + TestUtilities.checkRecipients(build1, new FirstFailingBuildSuspectsRecipientProvider(), "A"); - /* - * Requestor: A - * No committers. - * Unstable. - */ - final FreeStyleBuild build2 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); - PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build2, "A"); - TestUtilities.checkRecipients(build2, new FirstFailingBuildSuspectsRecipientProvider()); + /* + * Requestor: A + * No committers. + * Unstable. + */ + final FreeStyleBuild build2 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build2).getResult(); + Mockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build2, "A"); + TestUtilities.checkRecipients(build2, new FirstFailingBuildSuspectsRecipientProvider()); - /* - * Requestor: A - * Committers {X,V}. - * Failed. - */ - final FreeStyleBuild build3 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.FAILURE).when(build3).getResult(); - PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build3, "A"); - MockUtilities.addChangeSet(build3, "X", "V"); - TestUtilities.checkRecipients(build3, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); + /* + * Requestor: A + * Committers {X,V}. + * Failed. + */ + final FreeStyleBuild build3 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.FAILURE).when(build3).getResult(); + Mockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build3, "A"); + MockUtilities.addChangeSet(build3, "X", "V"); + TestUtilities.checkRecipients(build3, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); - /* - * Requestor: B - * Committers {X}. - * Aborted. - */ - final FreeStyleBuild build4 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.ABORTED).when(build4).getResult(); - PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build4, "B"); - MockUtilities.addChangeSet(build4, "X"); - TestUtilities.checkRecipients(build4, new FirstFailingBuildSuspectsRecipientProvider()); + /* + * Requestor: B + * Committers {X}. + * Aborted. + */ + final FreeStyleBuild build4 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.ABORTED).when(build4).getResult(); + Mockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build4, "B"); + MockUtilities.addChangeSet(build4, "X"); + TestUtilities.checkRecipients(build4, new FirstFailingBuildSuspectsRecipientProvider()); - /* - * Requestor: B - * Committers {U,V}. - * Failed. - */ - final FreeStyleBuild build5 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.FAILURE).when(build5).getResult(); - PowerMockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build5, "B"); - MockUtilities.addChangeSet(build5, "U", "V"); - TestUtilities.checkRecipients(build5, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); + /* + * Requestor: B + * Committers {U,V}. + * Failed. + */ + final FreeStyleBuild build5 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.FAILURE).when(build5).getResult(); + Mockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build5, "B"); + MockUtilities.addChangeSet(build5, "U", "V"); + TestUtilities.checkRecipients(build5, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); - /* - * Requestor: A - * Committers {W}. - * Success. - */ - final FreeStyleBuild build6 = PowerMockito.spy(new FreeStyleBuild(p)); - PowerMockito.doReturn(Result.UNSTABLE).when(build6).getResult(); - PowerMockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); - MockUtilities.addRequestor(build6, "A"); - MockUtilities.addChangeSet(build6, "W"); - TestUtilities.checkRecipients(build6, new FirstFailingBuildSuspectsRecipientProvider()); + /* + * Requestor: A + * Committers {W}. + * Success. + */ + final FreeStyleBuild build6 = Mockito.spy(new FreeStyleBuild(p)); + Mockito.doReturn(Result.UNSTABLE).when(build6).getResult(); + Mockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); + MockUtilities.addRequestor(mockedUser, build6, "A"); + MockUtilities.addChangeSet(build6, "W"); + TestUtilities.checkRecipients(build6, new FirstFailingBuildSuspectsRecipientProvider()); + } } } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java index f3b8ec155..4c6edf8ef 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java @@ -34,9 +34,9 @@ import hudson.tasks.test.AbstractTestResultAction; import org.apache.commons.collections.iterators.TransformIterator; import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import java.util.Arrays; import java.util.Collection; @@ -52,9 +52,9 @@ private MockUtilities() { } public static User getUser(final String author) { - final User user = PowerMockito.mock(User.class); + final User user = Mockito.mock(User.class); final Mailer.UserProperty mailProperty = new Mailer.UserProperty(author + AT_DOMAIN); - PowerMockito.when(user.getProperty(Mailer.UserProperty.class)).thenReturn(mailProperty); + Mockito.when(user.getProperty(Mailer.UserProperty.class)).thenReturn(mailProperty); return user; } @@ -110,36 +110,35 @@ public static ChangeLogSet makeChangeSet(final Run bui public static void addChangeSet(final WorkflowRun build, final String... inAuthors) { ChangeLogSet changeSet = makeChangeSet(build, inAuthors); - PowerMockito.when(build.getChangeSets()).thenReturn(Collections.singletonList(changeSet)); + Mockito.when(build.getChangeSets()).thenReturn(Collections.singletonList(changeSet)); } public static void addChangeSet(final AbstractBuild build, final String... inAuthors) { ChangeLogSet changeSet = makeChangeSet(build, inAuthors); - PowerMockito.doReturn(changeSet).when(build).getChangeSet(); + Mockito.doReturn(changeSet).when(build).getChangeSet(); } - public static void addRequestor(final AbstractBuild build, final String requestor) throws Exception { - PowerMockito.spy(User.class); - PowerMockito.doAnswer((Answer) invocation -> { + public static void addRequestor(final MockedStatic mockedUser, final AbstractBuild build, final String requestor) throws Exception { + mockedUser.when(() -> User.get(Mockito.anyString(), Mockito.anyBoolean(), Mockito.any())).then((Answer) invocation -> { Object[] args = invocation.getArguments(); return getUser((String) args[0]); - }).when(User.class, "get", Mockito.anyString(), Mockito.anyBoolean(), Mockito.any()); - final Cause.UserIdCause cause = PowerMockito.mock(Cause.UserIdCause.class); - PowerMockito.when(cause.getUserId()).thenReturn(requestor); - PowerMockito.doReturn(cause).when(build).getCause(Cause.UserIdCause.class); + }); + final Cause.UserIdCause cause = Mockito.mock(Cause.UserIdCause.class); + Mockito.when(cause.getUserId()).thenReturn(requestor); + Mockito.doReturn(cause).when(build).getCause(Cause.UserIdCause.class); } public static void addTestResultAction(final AbstractBuild build, final AbstractBuild... failedSinces) { final List failedTests = new LinkedList<>(); for (final AbstractBuild failedSince : failedSinces) { - final CaseResult caseResult = PowerMockito.mock(CaseResult.class); - PowerMockito.when(caseResult.getFailedSinceRun()).thenReturn(failedSince); + final CaseResult caseResult = Mockito.mock(CaseResult.class); + Mockito.when(caseResult.getFailedSinceRun()).thenReturn(failedSince); failedTests.add(caseResult); } - final TestResultAction testResultAction = PowerMockito.mock(TestResultAction.class); - PowerMockito.when(testResultAction.getFailedTests()).thenReturn(failedTests); - PowerMockito.when(testResultAction.getFailCount()).thenReturn(failedTests.size()); - PowerMockito.doReturn(testResultAction).when(build).getAction(AbstractTestResultAction.class); + final TestResultAction testResultAction = Mockito.mock(TestResultAction.class); + Mockito.when(testResultAction.getFailedTests()).thenReturn(failedTests); + Mockito.when(testResultAction.getFailCount()).thenReturn(failedTests.size()); + Mockito.doReturn(testResultAction).when(build).getAction(AbstractTestResultAction.class); } } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilitiesTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilitiesTest.java index fe48fd151..57071d6a8 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilitiesTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilitiesTest.java @@ -7,10 +7,6 @@ import org.hamcrest.collection.IsCollectionWithSize; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import java.util.Arrays; import java.util.Collections; @@ -18,15 +14,9 @@ import java.util.Set; import static org.hamcrest.MatcherAssert.assertThat; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - WorkflowRun.class -}) -@PowerMockIgnore({"javax.xml.*"}) // workaround inspired by https://github.com/powermock/powermock/issues/864#issuecomment-410182836 public class RecipientProviderUtilitiesTest { public static class Debug implements RecipientProviderUtilities.IDebug {