我试图模拟对象,但失败。
Class A {
protected SomeResponse someRespsoonse;
public SomeResponse mapping(){
someResponse = new SomeResponse();
return someResponse ;
}
}
然后我需要用下面的类测试它:
@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(A.class)
class ATest{
@Mock
A a = Mocktio.mock(A.class);
@Mock
SomeResponse someResponse = Mockito.mock(SomeResponse.class);
@Test
testMyResponse{
someResponse.setErrorInfo("500");
PowerMockito.whenNew(SomeResponse.class).withNoArguments().thenReturn(someResponse);
a.mapping();
// some blah blah
}}
问题是无法从测试类填充somerresponse对象。我浏览了Stackoverflow和google。但是找不到我想要的东西
您正在使用powermockit的whenNew(),因此将您的RunWith Runner从mockit更改为PowerMock。
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
class ATest{