【发布时间】:2017-05-01 22:15:15
【问题描述】:
使用 Spring Boot 启动器测试来测试我的应用程序,但我使用的是第三方库。假设我们有一个 TRequest 类,它有一些构造函数,我想模拟和存根该构造函数以返回结果。
@SpringBootTest
@RunWith(SpringRunner.class)
@PrepareForEverythingForTest
public class TestClass {
@MockBean
TRequest trequest ;
@Before
public void setUp() throws Exception {
PowerMockito.whenNew(TRequest.class).withAnyArguments().thenReturn(trequest);
}
}
现在,当我尝试使用 new 创建构造函数时,它没有返回正确的存根结果。
TRequest trequest1 = new TRequest("apiKey","secretKey") ;
trequest.equals(trequest1) ; // false but I want it to be true
【问题讨论】:
-
您可能需要在您的测试中使用
PowerMockitoRunner,然后才能拦截该构造。如果你想用 spring 运行它,也可以看看@PowerMockRunnerDelegate。
标签: spring unit-testing junit spring-boot powermock