Emilio Gumayagay
Emilio Gumayagay

Reputation: 69

Trying to test static method using powermock

I'm trying to do a test on a code which requires to mock some static methods. I found in the that using powermock, I will be able to mock the static methods being used.

I tried adding these dependencies power mock module-junit4 dependency as well as powermock-api-mockito and setting their versions explicitly to 1.7.1:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>1.7.1</version>
</dependency>

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-api-mockito</artifactId>
  <version>1.7.1</version>
  <scope>test</scope>
</dependency>

@RunWith(PowerMockRunner.class)
@PrepareForTest({SystemParameterQueryHelper.class})
public class VccTemplateConvertorTest{}

Exception in thread "main" java.lang.NoSuchMethodError: org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getFrameworkReporterFactory()Lorg/powermock/core/reporter/MockingFrameworkReporterFactory; at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getMockingFrameworkReporter(JUnit4TestSuiteChunkerImpl.java:140) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:119) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Upvotes: 0

Views: 1270

Answers (1)

Qword
Qword

Reputation: 90

You are using a fairly dated version of PowerMock. Looking at the documentation:

PowerMock version 2.0.0 and upper has support of Mockito 2. PowerMock version 1.7.0 and upper has experimental support of Mockito 2.

In this case emphasis is on experimental. I would suggest to try and update the PowerMock version you're using. This is my current configuration:

<properties>
  ...

  <version.mockito>2.23.4</version.mockito>
  <version.powermock>2.0.2</version.powermock>
</properties>

<dependencies>
   ...

  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>${version.mockito}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-core</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Upvotes: 0

Related Questions