我想测试ClassA
的方法,这些方法的构造函数使用ClassB
的静态方法。
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);
}
运行测试时,我得到一个NullPointerException,如:
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)
我不知道如何模拟构造函数ClassA
和ClassB
的静态方法来测试ClassA
的方法。
答案 0 :(得分:0)
我认为,您应该在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);
}