Skip to content

Commit

Permalink
Merge pull request #311 from basil/powermock
Browse files Browse the repository at this point in the history
Remove PowerMock
  • Loading branch information
basil authored Sep 27, 2021
2 parents 385e4d8 + e49836c commit a393943
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 353 deletions.
14 changes: 2 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>4.25</version>
<version>4.26</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -237,17 +237,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -257,7 +247,7 @@ public void testGetContent_withMessage_withStack_htmlEscaped() throws Exception
when(result.getErrorDetails()).thenReturn("expected:<ABORTED> but was:<COMPLETED> ");
when(result.getErrorStackTrace()).thenReturn("at org.nexusformat.NexusFile.<clinit>(NexusFile.java:99)");

Mockito.<List<? extends TestResult>>when(testResults.getFailedTests()).thenReturn(singletonList(result));
Mockito.<List<? extends TestResult>>when(testResults.getFailedTests()).thenReturn(Collections.singletonList(result));
when(build.getAction(AbstractTestResultAction.class)).thenReturn(testResults);

failedTestContent.showMessage = true;
Expand Down Expand Up @@ -347,7 +337,7 @@ public void testGetContent_withMessage_withStack_outputYaml() throws Exception {
when(result.getErrorDetails()).thenReturn("expected:<ABORTED> but was:<COMPLETED> ");
when(result.getErrorStackTrace()).thenReturn(testStackTrace);

Mockito.<List<? extends TestResult>>when(testResults.getFailedTests()).thenReturn(singletonList(result));
Mockito.<List<? extends TestResult>>when(testResults.getFailedTests()).thenReturn(Collections.singletonList(result));
when(build.getAction(AbstractTestResultAction.class)).thenReturn(testResults);

failedTestContent.showMessage = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

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;
import hudson.tasks.test.AggregatedTestResultAction;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Action>()
{{
add( expectedAction );
}} );

assertSame( expectedAction, buildWrapper.getAction( expectedAction.getClass().getName() ) );
}

@Test
public void testGetJUnitTestResult_whenMavenProjectUseMavenPluginsSurefireAggregatedReport()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Jenkins> mockedJenkins;
private MockedStatic<Mailer> 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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Jenkins> mockedJenkins;
private MockedStatic<Mailer> 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<User> 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");
}
Expand Down
Loading

0 comments on commit a393943

Please sign in to comment.