春季启动测试

编程入门 行业动态 更新时间:2024-10-22 11:22:58
本文介绍了春季启动测试-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()); } }

更多推荐

春季启动测试

本文发布于:2023-10-31 07:32:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:春季   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!