如何使用PowerMock测试方法Jsoup.connect(静态)?

编程入门 行业动态 更新时间:2024-10-27 08:34:13
本文介绍了如何使用PowerMock测试方法Jsoup.connect(静态)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的代码:

public void analyze(String url) throws SiteBusinessException { Document doc = null; Response response = null; try { response = Jsoup.connect(url).execute(); doc = Jsoup.connect(url).get(); } catch (IOException e) { LOGGER.warn("Cannot analyze site [url={}, statusCode={}, statusMessage={} ]", new Object[] {url, response.statusCode(), response.statusMessage()}); throw new SiteBusinessException(response.statusMessage(), String.valueOf(response.statusCode())); } }

如何使用PowerMock测试此方法?我想编写测试以检查在调用.execute()时是否抛出IOException并捕获并随后抛出SiteBusinessException.

How can I test this method using PowerMock? I want to write test to check that when invoke .execute() then throw IOException and it catch then throw SiteBusinessException.

我的测试代码.

@RunWith(PowerMockRunner.class) @PrepareForTest({Jsoup.class}) Test(expected = SiteBusinessException.class) public void shouldThrowIOException() throws Exception { Connection connection = PowerMockito.mock(Connection.class); Response response = PowerMockito.mock(Response.class); PowerMockito.when(connection.execute()).thenReturn(response); PowerMockito.mockStatic(Jsoup.class); expect(Jsoup.connect(SITE_URL)).andReturn(connection); replay(Jsoup.class); PowerMockito.when(Jsoup.connect(SITE_URL).execute()).thenThrow(new IOException()); AnalyzerService sut = new AnalyzerServiceImpl(); sut.analyzeSite(SITE_URL); }

我知道了

java.lang.Exception: Unexpected exception, expected<com.siteraport.exception.SiteBusinessException> but was<java.lang.IllegalStateException>

??

推荐答案

您需要创建Jsoup类的静态模拟.一旦在测试用例中创建了这样的模拟,就可以使用它来编写期望值.

You need to create a static mock of the Jsoup class. Once you have created such a mock in your test case, you can code your expectations using it.

请参阅使用PowerMockito模拟静态方法文档.

在这里使用Mockito和PowerMockito测试用例:

Here the Testcase using Mockito and PowerMockito:

我能够使用Mockito + Powermockito模拟执行方法(您同时使用EasyMock和Mockito吗?)测试用例中的代码如下:

I was able to mock the execute method using Mockito + Powermockito (you are using both EasyMock and Mockito?) The code in the test case looks as below:

@RunWith(PowerMockRunner.class) @PrepareForTest({Jsoup.class}) public class MyClassTest { @Test(expected = SiteBusinessException.class) public void shouldThrowIOException() throws Exception { String SITE_URL = "some_url_string"; Connection connection = Mockito.mock(Connection.class); Mockito.when(connection.execute()).thenThrow(new IOException("test")); PowerMockito.mockStatic(Jsoup.class); PowerMockito.when(Jsoup.connect(Mockito.anyString())). thenReturn(connection); AnalyzerService sut = new AnalyzerService(); sut.analyze(SITE_URL); } }

更多推荐

如何使用PowerMock测试方法Jsoup.connect(静态)?

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

发布评论

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

>www.elefans.com

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