PowerMock,模拟使用静态方法抛出空指针异常的类的方法



我想测试具有使用ClassB静态方法的构造函数的ClassA的方法。

public class ClassA
{
    private String something;
    private ClassC somethingElse;
    public ClassA(String parameter)
    {
        super(parameter);
        setClassC(ClassB.getClassC());
    }
    public ReturnClass methodToTest()
    {
        // do something...
    }
}
public class ClassB
{
    public static ClassC getClassC()
    {
        // do something and return ClassC
    }
}

测试类

@Test
@PrepareForTest(ClassB.class)
public void test_methodToTest() throws Exception 
{
    //Given
    parameter = Mockito.mock(String.class);
    aClassC = PowerMockito.mock(ClassC.class);
PowerMockito.mockStatic(ClassB.class);
    ClassA aClassA = new ClassA(parameter);
    //When
    PowerMockito.when(ClassB.getClassC()).thenReturn(aClassC);
    // Some behavior to test the method
    //Then
    ReturnClass aReturn = aClassA.methodToTest();
    assertNull(aReturn);
}

运行测试时,我得到一个空指针异常,如下所示:

例外

Java.lang.NullPointerException
    at ClassB.getClassC(ClassB.java:285)
    at ClassA.<init>(ClassA.java:72)
    at ClassATest.test_methodToTest(ClassATest.java:56)
    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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)

我不知道如何模拟构造函数ClassAClassB的静态方法来测试ClassA的方法。

我认为

,你应该把PowerMockito.when放在lassA aClassA = new ClassA(parameter);之前

@Test
@PrepareForTest(ClassB.class)
public void test_methodToTest() throws Exception 
{
    //Given
    parameter = Mockito.mock(String.class);
    aClassC = PowerMockito.mock(ClassC.class);
    PowerMockito.mockStatic(ClassB.class);
    //When
    PowerMockito.when(ClassB.getClassC()).thenReturn(aClassC); 
    ClassA aClassA = new ClassA(parameter);
    // Some behavior to test the method
    //Then
    ReturnClass aReturn = aClassA.methodToTest();
    assertNull(aReturn);
}

相关内容

  • 没有找到相关文章

最新更新