【发布时间】:2025-12-27 04:15:06
【问题描述】:
我试图模拟对象但失败了。
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
}}
问题无法从测试类填充 SomeResponse 对象。 我浏览了 * 和谷歌。但找不到想要的东西。
【问题讨论】:
标签: unit-testing mockito