如何使用由静态初始化程序调用的PowerMockito来模拟java中的私有静态方法?

时间:2016-11-01 02:53:10

标签: java static testng initializer powermockito

我试图模仿下面的静态方法,但我被静态初始化程序阻止了。见下面的代码:

public class StaticInitializerService {

static{
    init();
}

private static void init(){
    throw new UnsupportedOperationException();
}

private static String getString(){
    throw new UnsupportedOperationException();
}

public static String method(){
    return getString();
}
}

这是我的测试用例:

@PrepareForTest(StaticInitializerService.class)
public class StaticInitializerServiceTest   extends PowerMockTestCase{
@ObjectFactory
public ITestObjectFactory getObjectFactory() {
    return new PowerMockObjectFactory();
}

@Test
public void method() {
  PowerMockito.mockStatic(StaticInitializerService.class);
}
}

我尝试使用mockstatic和间谍,这都引发了以下异常:

java.lang.ExceptionInInitializerError
at sun.reflect.GeneratedSerializationConstructorAccessor4.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:14)
at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxy(ClassImposterizer.java:143)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:58)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59)
at org.powermock.api.mockito.PowerMockito.spy(PowerMockito.java:234)
at com.zghome.mvndemo.TestNGDemo.powermock.service.testng.StaticInitializerServiceTest.method(StaticInitializerServiceTest.java:22)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
Caused by: java.lang.UnsupportedOperationException
at     com.zghome.mvndemo.TestNGDemo.powermock.service.StaticInitializerService.init(St aticInitializerService.java:10)
at com.zghome.mvndemo.TestNGDemo.powermock.service.StaticInitializerService.  <clinit>(StaticInitializerService.java:6)
... 35 more

我想我不能这样嘲笑这个课程,但我不知道怎么嘲笑这个。任何想法如何解决它?

1 个答案:

答案 0 :(得分:3)

请添加以下行以避免上述异常

@SuppressStaticInitializationFor("packageName.StaticInitializerService")

然后,其余的模拟将类似于静态void方法模拟和PowerMockito.doThrow()方法来提示异常。这样的示例代码不会为您的类抛出任何异常

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
    import org.powermock.modules.junit4.PowerMockRunner;
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(StaticInitializerService.class)
    @SuppressStaticInitializationFor("PackageName.StaticInitializerService")
    public class StaticInitializerServiceTest {
    @Test 
    public void method() {
    PowerMockito.mockStatic(StaticInitializerService.class);
    }   
  }