Repository: vackosar/gitflow-incremental-builder
Pull request #115 (2020-01-20 22:51:20)

 Issue #114: upgrade to mockito 3.2.4 and powermock-reflect 2.0.4


Patch:
diff --git a/pom.xml b/pom.xml
index 9df6dbc..81fc0f7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,8 +63,8 @@
         <version.logback>1.1.3</version.logback>    <!-- the latest that appears to work properly with slf4j 1.7.5 -->
         <version.javax.inject>1</version.javax.inject>
         <version.junit>4.13</version.junit>
-        <version.powermock-reflect>1.7.4</version.powermock-reflect>
-        <version.mockito-all>1.10.19</version.mockito-all>
+        <version.powermock-reflect>2.0.4</version.powermock-reflect>
+        <version.mockito>3.2.4</version.mockito>
         <version.jetty>9.4.26.v20200117</version.jetty>
 
         <!-- Plugin versions -->
@@ -153,8 +153,8 @@
         </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
-            <artifactId>mockito-all</artifactId>
-            <version>${version.mockito-all}</version>
+            <artifactId>mockito-core</artifactId>
+            <version>${version.mockito}</version>
             <scope>test</scope>
         </dependency>
         <dependency>


Test patch:
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/LoggerSpyUtil.java b/src/test/java/com/vackosar/gitflowincrementalbuild/LoggerSpyUtil.java
index d24fdfe..5e4a62b 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/LoggerSpyUtil.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/LoggerSpyUtil.java
@@ -1,17 +1,16 @@
 package com.vackosar.gitflowincrementalbuild;
 
+import org.mockito.AdditionalAnswers;
 import org.mockito.Mockito;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.vackosar.gitflowincrementalbuild.mocks.MockitoWorkarounds;
-
 public class LoggerSpyUtil {
 
     public static Logger buildSpiedLoggerFor(Class<?> owningClazz) {
         Logger logger = LoggerFactory.getLogger(owningClazz);
         // note: cannot directly use Mockito.spy(logger) because logback loggers are final
-        Logger spiedLogger = Mockito.mock(Logger.class, MockitoWorkarounds.AdditionalAnswers.delegatesTo(logger));
+        Logger spiedLogger = Mockito.mock(Logger.class, AdditionalAnswers.delegatesTo(logger));
         return spiedLogger;
     }
 }
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
index 7cc9032..e511780 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
@@ -1,9 +1,13 @@
 package com.vackosar.gitflowincrementalbuild.boundary;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -19,15 +23,12 @@ import java.util.regex.PatternSyntaxException;
 import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.project.MavenProject;
-import org.hamcrest.core.IsInstanceOf;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.RuleChain;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
 
 import com.google.common.collect.ImmutableMap;
 import com.vackosar.gitflowincrementalbuild.SystemPropertiesResetRule;
@@ -43,10 +44,8 @@ import com.vackosar.gitflowincrementalbuild.control.Property;
 @RunWith(MockitoJUnitRunner.class)
 public class ConfigurationTest {
 
-    private final ExpectedException thrown = ExpectedException.none();
-
     @Rule
-    public final RuleChain ruleChain = RuleChain.outerRule(new SystemPropertiesResetRule()).around(thrown);
+    public final SystemPropertiesResetRule sysPropResetRule = new SystemPropertiesResetRule();
 
     @Mock
     private MavenExecutionRequest mavenExecutionRequestMock;
@@ -69,11 +68,12 @@ public class ConfigurationTest {
     public void invalidProperty() {
         String invalidProperty = Property.PREFIX + "invalid";
         System.setProperty(invalidProperty, "invalid");
-        thrown.expect(IllegalArgumentException.class);
-        thrown.expectMessage(invalidProperty);
-        thrown.expectMessage(Property.disableBranchComparison.fullName());  // just one of those valid ones
 
-        new Configuration.Provider(mavenSessionMock).get();
+        IllegalArgumentException expectedException = assertThrows(IllegalArgumentException.class,
+                () -> new Configuration.Provider(mavenSessionMock).get());
+
+        assertThat(expectedException.getMessage(), containsString(invalidProperty));
+        assertThat(expectedException.getMessage(), containsString(Property.disableBranchComparison.fullName())); // just one of those valid ones
     }
 
     @Test
@@ -143,11 +143,12 @@ public class ConfigurationTest {
     @Test
     public void forceBuildModules_patternInvalid() {
         System.setProperty(Property.forceBuildModules.fullName(), "*-some-artifact");   // pattern is missing the dot
-        thrown.expect(IllegalArgumentException.class);
-        thrown.expectMessage(Property.forceBuildModules.fullName());
-        thrown.expectCause(IsInstanceOf.instanceOf(PatternSyntaxException.class));
 
-        new Configuration.Provider(mavenSessionMock).get();
+        IllegalArgumentException expectedException = assertThrows(IllegalArgumentException.class,
+                () -> new Configuration.Provider(mavenSessionMock).get());
+
+        assertThat(expectedException.getMessage(), containsString(Property.forceBuildModules.fullName()));
+        assertThat(expectedException.getCause(), instanceOf(PatternSyntaxException.class));
     }
 
     // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -196,10 +197,11 @@ public class ConfigurationTest {
     @Test
     public void buildUpstreamMode_unknown() {
         System.setProperty(Property.buildUpstream.fullName(), "foo");
-        thrown.expect(IllegalArgumentException.class);
-        thrown.expectMessage(Property.buildUpstream.fullName());
 
-        new Configuration.Provider(mavenSessionMock).get();
+        IllegalArgumentException expectedException = assertThrows(IllegalArgumentException.class,
+                () -> new Configuration.Provider(mavenSessionMock).get());
+
+        assertThat(expectedException.getMessage(), containsString(Property.buildUpstream.fullName()));
     }
 
     // tests for mode value 'derived' (default value)
@@ -252,11 +254,12 @@ public class ConfigurationTest {
     public void buildUpstreamMode_derived_makeUpstream_unknown() {
         System.setProperty(Property.buildUpstreamMode.fullName(), "foo");
         when(mavenExecutionRequestMock.getMakeBehavior()).thenReturn(MavenExecutionRequest.REACTOR_MAKE_UPSTREAM);
-        thrown.expect(IllegalArgumentException.class);
-        thrown.expectMessage(Property.buildUpstreamMode.fullName());
-        thrown.expectCause(IsInstanceOf.instanceOf(IllegalArgumentException.class));
 
-        new Configuration.Provider(mavenSessionMock).get();
+        IllegalArgumentException expectedException = assertThrows(IllegalArgumentException.class,
+                () -> new Configuration.Provider(mavenSessionMock).get());
+
+        assertThat(expectedException.getMessage(), containsString(Property.buildUpstreamMode.fullName()));
+        assertThat(expectedException.getCause(), instanceOf(IllegalArgumentException.class));
     }
 
     // just an example to show 'derived' can also be set explicitely
@@ -325,10 +328,11 @@ public class ConfigurationTest {
     @Test
     public void buildDownstream_unknown() {
         System.setProperty(Property.buildDownstream.fullName(), "foo");
-        thrown.expect(IllegalArgumentException.class);
-        thrown.expectMessage(Property.buildDownstream.fullName());
 
-        new Configuration.Provider(mavenSessionMock).get();
+        IllegalArgumentException expectedException = assertThrows(IllegalArgumentException.class,
+                () -> new Configuration.Provider(mavenSessionMock).get());
+
+        assertThat(expectedException.getMessage(), containsString(Property.buildDownstream.fullName()));
     }
 
     @Test
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
index 6e5c760..e3e5d54 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
@@ -9,29 +9,28 @@ import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.execution.ProjectDependencyGraph;
 import org.apache.maven.project.MavenProject;
-import org.hamcrest.core.IsSame;
 import org.junit.Before;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.powermock.reflect.Whitebox;
 import org.slf4j.Logger;
 
 import java.util.Properties;
 
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.contains;
-import static org.mockito.Matchers.eq;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.contains;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -39,9 +38,6 @@ public class MavenLifecycleParticipantTest {
 
     private static final String TEST_IMPL_VERSION = "3.8.1";    // just an existing version, no need to be the latest one
 
-    @Rule
-    public final ExpectedException thrown = ExpectedException.none();
-
     private Logger loggerSpy = LoggerSpyUtil.buildSpiedLoggerFor(MavenLifecycleParticipant.class);
 
     @Mock
@@ -84,7 +80,7 @@ public class MavenLifecycleParticipantTest {
         underTest.afterProjectsRead(mavenSessionMock);
 
         verify(loggerSpy).info(contains("disabled"));
-        verifyZeroInteractions(unchangedProjectsRemoverMock);
+        verifyNoInteractions(unchangedProjectsRemoverMock);
         verify(mavenSessionMock, never()).getProjectDependencyGraph();
     }
 
@@ -96,7 +92,7 @@ public class MavenLifecycleParticipantTest {
         underTest.afterProjectsRead(mavenSessionMock);
 
         verifyHelpLogged(true);
-        verifyZeroInteractions(unchangedProjectsRemoverMock);
+        verifyNoInteractions(unchangedProjectsRemoverMock);
     }
 
     @Test
@@ -121,10 +117,11 @@ public class MavenLifecycleParticipantTest {
     public void onRuntimeException() throws Exception {
         RuntimeException runtimeException = new RuntimeException("FAIL !!!");
         doThrow(runtimeException).when(unchangedProjectsRemoverMock).act();
-        thrown.expect(MavenExecutionException.class);
-        thrown.expectCause(IsSame.sameInstance(runtimeException));
 
-        underTest.afterProjectsRead(mavenSessionMock);
+        MavenExecutionException expectedException = assertThrows(MavenExecutionException.class,
+                () -> underTest.afterProjectsRead(mavenSessionMock));
+
+        assertSame(runtimeException, expectedException.getCause());
     }
 
     @Test
@@ -157,7 +154,7 @@ public class MavenLifecycleParticipantTest {
         underTest.afterProjectsRead(mavenSessionMock);
 
         verify(loggerSpy).warn(contains("ProjectDependencyGraph"));
-        verifyZeroInteractions(unchangedProjectsRemoverMock);
+        verifyNoInteractions(unchangedProjectsRemoverMock);
     }
 
     private void verifyHelpLogged(boolean logged) {
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/UnchangedProjectsRemoverTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/UnchangedProjectsRemoverTest.java
index c2bd05b..89551de 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/UnchangedProjectsRemoverTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/UnchangedProjectsRemoverTest.java
@@ -4,13 +4,12 @@
 package com.vackosar.gitflowincrementalbuild.boundary;
 
 import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.anyListOf;
+import static org.mockito.Mockito.anyList;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -35,7 +34,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.powermock.reflect.Whitebox;
 
 import com.google.common.collect.ImmutableMap;
@@ -79,7 +78,6 @@ public class UnchangedProjectsRemoverTest {
     @Before
     public void setup() throws GitAPIException, IOException {
         when(mavenProjectMock.getProperties()).thenReturn(projectProperties);
-        when(mavenProjectMock.getBasedir()).thenReturn(new File("."));
         when(mavenProjectMock.getArtifactId()).thenReturn(ARTIFACT_ID_1);
 
         when(mavenSessionMock.getCurrentProject()).thenReturn(mavenProjectMock);
@@ -283,7 +281,7 @@ public class UnchangedProjectsRemoverTest {
 
         underTest.act();
 
-        verify(mavenSessionMock, never()).setProjects(anyListOf(MavenProject.class));
+        verify(mavenSessionMock, never()).setProjects(anyList());
 
         assertProjectPropertiesEqual(mavenProjectMock, ImmutableMap.of("enforcer.skip", "true", "argWithNoValue", ""));
         assertProjectPropertiesEqual(changedModuleMock, Collections.emptyMap());
@@ -336,7 +334,7 @@ public class UnchangedProjectsRemoverTest {
 
         underTest.act();
 
-        verify(mavenSessionMock, never()).setProjects(anyListOf(MavenProject.class));
+        verify(mavenSessionMock, never()).setProjects(anyList());
 
         assertProjectPropertiesEqual(mavenProjectMock, ImmutableMap.of("foo", "bar"));
         assertProjectPropertiesEqual(changedModuleMock, Collections.emptyMap());
@@ -454,7 +452,7 @@ public class UnchangedProjectsRemoverTest {
         underTest.act();
 
         verify(mavenSessionMock, never())
-                .setProjects(anyListOf(MavenProject.class));
+                .setProjects(anyList());
     }
 
     @Test
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/control/BaseChangedProjectsTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/control/BaseChangedProjectsTest.java
index 6dfde05..c37a520 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/control/BaseChangedProjectsTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/control/BaseChangedProjectsTest.java
@@ -11,7 +11,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Spy;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.powermock.reflect.Whitebox;
 
 import java.io.File;
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
index cfc1eeb..29f388a 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
@@ -1,6 +1,6 @@
 package com.vackosar.gitflowincrementalbuild.control;
 
-import static org.mockito.Matchers.contains;
+import static org.mockito.ArgumentMatchers.contains;
 import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/JGitIsolation.java b/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/JGitIsolation.java
index 078484a..428f003 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/JGitIsolation.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/JGitIsolation.java
@@ -1,8 +1,9 @@
 package com.vackosar.gitflowincrementalbuild.mocks;
 
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.withSettings;
 
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.storage.file.FileBasedConfig;
@@ -37,7 +38,9 @@ public class JGitIsolation {
         };
 
         // note: cannot directly use Mockito.spy(SystemReader.getInstance()) because org.eclipse.jgit.util.SystemReader.Default is invisible
-        SystemReader spiedSystemReader = mock(SystemReader.class, AdditionalAnswers.delegatesTo(SystemReader.getInstance()));
+        SystemReader spiedSystemReader = mock(SystemReader.class, withSettings()
+                .defaultAnswer(AdditionalAnswers.delegatesTo(SystemReader.getInstance()))
+                .lenient());
         doReturn(emptyConfig).when(spiedSystemReader).openSystemConfig(any(Config.class), any(FS.class));
         doReturn(emptyConfig).when(spiedSystemReader).openUserConfig(any(Config.class), any(FS.class));
         SystemReader.setInstance(spiedSystemReader);
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MavenSessionMock.java b/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MavenSessionMock.java
index bd61977..f5b617a 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MavenSessionMock.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MavenSessionMock.java
@@ -21,6 +21,7 @@ import java.util.stream.Collectors;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
 
 public class MavenSessionMock {
 
@@ -36,7 +37,7 @@ public class MavenSessionMock {
 
         MavenProject topLevelProject = projects.get(0);
         topLevelProject.getModel().setProperties(topLevelProjectProperties);
-        MavenSession mavenSession = mock(MavenSession.class);
+        MavenSession mavenSession = mock(MavenSession.class, withSettings().lenient());
         when(mavenSession.getCurrentProject()).thenReturn(topLevelProject);
         MavenExecutionRequest request = mock(MavenExecutionRequest.class);
         when(mavenSession.getRequest()).thenReturn(request);
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MockitoWorkarounds.java b/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MockitoWorkarounds.java
deleted file mode 100644
index 65d5d88..0000000
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/mocks/MockitoWorkarounds.java
+++ /dev/null
@@ -1,96 +0,0 @@
-package com.vackosar.gitflowincrementalbuild.mocks;
-
-import java.lang.reflect.Method;
-
-import org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations;
-import org.mockito.invocation.Invocation;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-/**
- * Provides workarounds for bugs in Mockito.
- */
-public class MockitoWorkarounds {
-
-    public static class AdditionalAnswers {
-
-        // workaround for https://github.com/mockito/mockito/issues/407 based on https://github.com/mockito/mockito/pull/412/files 
-        @SuppressWarnings("unchecked")
-        public static <T> Answer<T> delegatesTo(Object delegate) {
-            return (Answer<T>) new PatchedForwardsInvocations(delegate);
-        }
-
-        private static class PatchedForwardsInvocations extends ForwardsInvocations {
-
-            private static final long serialVersionUID = 1L;
-
-            public PatchedForwardsInvocations(Object delegatedObject) {
-                super(delegatedObject);
-            }
-
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                return super.answer(new InvocationOnMockWrapper(invocation) {
-
-                    private static final long serialVersionUID = 1L;
-
-                    @Override
-                    public Object[] getArguments() {
-                        return ((Invocation) invocation).getRawArguments();
-                    }
-                });
-            }
-        }
-    }
-
-    private static class InvocationOnMockWrapper implements InvocationOnMock {
-
-        private static final long serialVersionUID = 1L;
-
-        private final InvocationOnMock delegate;
-
-        public InvocationOnMockWrapper(InvocationOnMock delegate) {
-            this.delegate = delegate;
-        }
-
-        @Override
-        public Object getMock() {
-            return delegate.getMock();
-        }
-
-        @Override
-        public Method getMethod() {
-            return delegate.getMethod();
-        }
-
-        @Override
-        public Object[] getArguments() {
-            return delegate.getArguments();
-        }
-
-        @Override
-        public <T> T getArgumentAt(int index, Class<T> clazz) {
-            return delegate.getArgumentAt(index, clazz);
-        }
-
-        @Override
-        public Object callRealMethod() throws Throwable {
-            return delegate.callRealMethod();
-        }
-
-        @Override
-        public int hashCode() {
-            return delegate.hashCode();
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            return delegate.equals(obj);
-        }
-
-        @Override
-        public String toString() {
-            return delegate.toString();
-        }
-    }
-}