Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mock a static method on a final class using a PowerMockRule instead of the PowerMockRunner?

According to the PowerMock docs, I should be able to run using a PowerMockRule instead of @RunWith(PowerMockRunner.class) and get the same results.

I seem to have found a case where this isn't true.

The below sample runs fine:

package com.test.powermockstatics;

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

final class FinalClassWithStaticCall {
  public static int getIntStatic() {
    return 1;
  }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(FinalClassWithStaticCall.class)
public class TestStaticMockingWithoutPowerMockRunner {
  @Test
  public void testStaticCall() {
    mockStatic(FinalClassWithStaticCall.class);
    when(FinalClassWithStaticCall.getIntStatic()).thenReturn(2);

    assertEquals(FinalClassWithStaticCall.getIntStatic(), 2);
  }
}

But when switched to a rule like so:

package com.test.powermockstatics;

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Rule;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.agent.PowerMockAgent;
import org.powermock.modules.junit4.rule.PowerMockRule;

final class FinalClassWithStaticCall {
  public static int getIntStatic() {
    return 1;
  }
}

@PrepareForTest(FinalClassWithStaticCall.class)
public class TestStaticMockingWithoutPowerMockRunner {
  static {
    PowerMockAgent.initializeIfNeeded();
  }

  @Rule
  public PowerMockRule rule = new PowerMockRule();

  @Test
  public void testStaticCall() {
    mockStatic(FinalClassWithStaticCall.class);
    when(FinalClassWithStaticCall.getIntStatic()).thenReturn(2);

    assertEquals(FinalClassWithStaticCall.getIntStatic(), 2);
  }
}

I get the following exception:

java.lang.IllegalArgumentException: Cannot subclass final class class com.test.powermockstatics.FinalClassWithStaticCall at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70) at com.test.powermockstatics.TestStaticMockingWithoutPowerMockRunner.testStaticCall(TestStaticMockingWithoutPowerMockRunner.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I am following the recommendation from the docs to:

put powermock-module-junit4-rule-agent before junit in the classpath

Does anyone know the official word if this is a bug in PowerMock or the desired behavior (i.e., you simply can't mock a static method on a final class using a PowerMockRule)?

EDIT:

Please see the clarifying details in the comments under Gábor Lipták's answer. I do not want to use a statically loaded Agent, since it appears the dynamically loaded Agent ought to be capable of getting the job done?

I know starting the agent statically will work. (Unfortunately this is not an option in my project.) So does anyone know if the failure of the dynamically loaded Agent is a bug in PowerMock? Or a known limitation; and why?

like image 374
Tom Tresansky Avatar asked Sep 15 '14 14:09

Tom Tresansky


People also ask

Can you mock static methods?

Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.

Why we should not use PowerMock?

Avoiding powermock in the early stage will make code better instead of creating tech debt with bad code. Using Powermock in big applications makes testing all the more difficult. Initially powermock documentation itself mentions — “compromise with good design”.

What is the difference between PowerMock and Mockito?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.


2 Answers

You need to prepare the class for test!

@PrepareForTest(MyFinalClass.class)

like image 106
arush436 Avatar answered Nov 14 '22 11:11

arush436


For mocking final classes classpath is not enough. You need JVM agent.

According to the docs:

In some cases (such as mocking final classes) it may be necessary to load the PowerMock agent eagerly in Maven in order for the tests to work in Surefire. If you experience this please add the following to your pom.xml:

Needed JVM argument to mock final classes:

-javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/1.5.6/powermock-module-javaagent-1.5.6.jar
like image 31
Gábor Lipták Avatar answered Nov 14 '22 11:11

Gábor Lipták