3

I am using powermockito to mock static methods with params in Java 7. Recently started migrating to Java 8. Post migration the powermockito stopped mocking the static methods and started calling the original method.

Pom.xml

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

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4-rule-agent</artifactId>
    <version>1.6.5</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.2-GA</version>
    <scope>test</scope>        
</dependency>

Test class
using spring runner with PowerMockRule
@RunWith(SpringJUnit4ClassRunner.class)

@Rule
public PowerMockRule rule = new PowerMockRule(); 

Below is the code used for mock/stubbing the static..

PowerMockito.stub(PowerMockito.method(ABCHelper.class, "prepareResult",Arg1.class,Arg2.class,Arg3.class,Arg4.class)).toReturn(mockedReturnedObject);

Can somebody please help me with this issue?

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45

1 Answers1

0

Below code works for me. Please give it a try with below dependencies and code in new project.

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>PowerMockTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-classloading-xstream</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

Test class

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Rule;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;

@PrepareForTest({UtilTest.class})
public class PowerMockRuleTest {

  @Rule
  public PowerMockRule powerMockRule = new PowerMockRule();

  @Test
  public void testStatic() throws Exception{
    PowerMockito.mockStatic(UtilTest.class);
    when(UtilTest.getName()).thenReturn("1234");
    System.out.println(UtilTest.getName());

    Main mainMock = mock(Main.class);

    PowerMockito.whenNew(Main.class).withAnyArguments().thenReturn(mainMock);
    when(mainMock.getMyTest()).thenReturn("12345");
    System.out.println(mainMock.getMyTest());
  }

}

Classess:

public class UtilTest {

  public static String getName() {

    return "test";
  }

}


public class Main {

  public static void main(String[] args) {
    System.out.println("Hello World!");
  }


  public String getMyTest() {
    return "main test";
  }
}
CNKR
  • 568
  • 5
  • 19