我正在尝试模拟一个在另一个方法中递归调用的方法。
public class ABC
{
public static Object getSessionAttribute(String value) {
return Context.current().getHttpSession().getAttribute(value);
}
public static String myMethod(String xyz) throws Exception{
/*Some important business logic*/
String abc = getSessionAttribute("abcdefghj").toString();
/*Some important business logic*/
return abc;
}
}
我的测试课是这样的;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ABC.class)
public class ABCTest {
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(ABC.class, Mockito.CALLS_REAL_METHODS);
PowerMockito.when(ABC.getSessionAttribute("dummyValue")).thenReturn("myReturnValue");
}
@Test
public void testmyMethod() throws Exception {
Assert.assertEquals("anyValue", ABC.myMethod("value"));
}
}
在这里,我从 ** return Context.current().getHttpSession().getAttribute(value)** 行得到一个 NullPointerException。我认为它希望我模拟从 Context 到 .getAttribute 的每个类。我怎样才能让它工作?