本文介绍了春季启动测试-PowerMockito模拟和存根构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Spring Boot Starter测试来测试我的应用程序,但是我正在使用第三方库.假设我们有一个TRequest类,它有一些构造函数,我想对该构造函数进行模拟和存根以返回结果.
Using Spring boot starter test for testing my application but I am using third party library. Lets suppose we have a class TRequest and it has some constructor and I want to mock and stub that constructor to return the result.
@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创建构造函数时,它没有返回正确的存根结果.
Now when I am trying to create the constructor using new, it is not returning the correct stubbed result.
TRequest trequest1 = new TRequest("apiKey","secretKey") ; trequest.equals(trequest1) ; // false but I want it to be true 推荐答案使用了 jackson 第三方库进行测试.-由于PowerMock而获得ClassLoader异常.
Have used a jackson third party lib to test with. - getting ClassLoader exceptions because of PowerMock though.
@SpringBootTest @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) public class TestPowerMockito { @MockBean ObjectMapper object; @Before public void init() throws Exception { PowerMockito.whenNew(ObjectMapper.class).withAnyArguments().thenReturn(object); } @Test public void test() { assertEquals(object, new ObjectMapper()); } }更多推荐
春季启动测试
发布评论