Reputation: 135
I am having troubles running a simple test that, accordingly with all the examples found in internet, should work without any issue.
I have a Java 8 Maven project with the following pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testingPowerMock</groupId>
<artifactId>constructorMocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestingPowerMock</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0-M1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
As I am using JUnit5, I can run tests only from command line using the SureFire plugin with the command: mvn clean test
I created these two very simple classes:
public class Created {
private String message;
public Created(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}
public class Creator {
public Created create() {
return new Created("Another message");
}
}
When I run the following test, it fails.
@RunWith(PowerMockRunner.class)
@PrepareForTest(Creator.class)
public class RunnerTest {
@Test
public void test() throws Exception {
Creator creator = new Creator();
Created created = mock(Created.class);
when(created.getMessage()).thenReturn("The expected message");
whenNew(Created.class).withAnyArguments().thenReturn(created);
Created aNewlyCreatedObject = creator.create();
assertEquals("The expected message", aNewlyCreatedObject.getMessage());
}
}
It looks like the constructor call is not intercepted as Mockito is not proxying it. I tried different version of the dependencies but with no luck. I also tried changing the code using:
whenNew(Created.class).withArguments("Another message").thenReturn(created);
or
whenNew(Created.class).withArguments(anyString()).thenReturn(created);
but none of these methods have changed the outcome of the test.
Could somebody help me with this issue please?
Thank you
Upvotes: 2
Views: 1341
Reputation: 2563
Apparently PowerMock does not support jUnit5 (yet?). Here is the epic for adding jUnit5 support. https://github.com/powermock/powermock/issues/830. It is too bad they don't have the resources to fix PowerMock because it is really a very powerful framework to make unit tests for legacy applications.
To my opinion when you need PowerMock to make a unit test, the class you try to test isn't written correctly and should be refactored to be testable. Only for legacy code this is sometimes too much to ask and therefor PowerMock gives you the ability to test an otherwise untestable class.
Upvotes: 0
Reputation: 135
It turned out that downgrading to JUnit4 solved the problem.
Below the final pom.xml to use to run the test successfully. Changing the PowerMock version seems to not have any effect as I tried with 1.6.6, 1.7.1 and 2.0.0-beta.5. I will post a bug to Powermock team.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testingPowerMock</groupId>
<artifactId>constructorMocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestingPowerMock</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.20.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Upvotes: 2