我有以下课程要测试:
public abstract class Challenge {
protected int id;
protected String name;
protected String question;
protected Challenge(){}
public String[] toStrings(){
String[] s = {Integer.toString(id), name, question};
return s;
}
...
但是使用这个测试:
@Test
public void testToStrings() throws Exception{
String[] expectedResult1 = new String[]{"1", "a", "b"};
String[] obtainedResult1 = null;
Challenge challengeMock = PowerMockito.mock(Challenge.class);
challengeMock.id = 1;
challengeMock.name = "a";
challengeMock.question = "b";
obtainedResult1 = challengeMock.toStrings();
Assert.assertEquals(expectedResult1[0], obtainedResult1[0]);
Assert.assertEquals(expectedResult1[1], obtainedResult1[1]);
Assert.assertEquals(expectedResult1[2], obtainedResult1[2]);
}
由于“obtainedResult1 = challengeMock.toStrings();”,我得到了 NullPointerException 返回null。
我使用带有规则的在 Robolectric 中运行的 PowerMock + Mockito(因为它是一个 Android 项目)。
@Rule
public PowerMockRule rule = new PowerMockRule();
哪里有问题?