如何在包含在非静态方法中的静态方法上正确使用 Mockito?
How to use Mockito properly on Static methods wrapped inside non-static methods?
所以我正在尝试在其中包含静态方法的方法上使用 Mockito。原因是我不能使用 PowerMock,所以我将方法包装在非静态方法下。
public class WrapperUtil {
public String getURLContent(String path) throws IOException{
URL url = new URL(path);
return IOUtils.toString(url);
}
}
现在我以两种不同的方式测试了 WrapperUtil class。一个测试有效,但没有为 WrapperUtil class 提供任何覆盖,另一个是抛出与静态方法相关的空指针异常。
这是有效的,但没有提供任何覆盖范围。
@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {
@InjectMocks
WrapperUtil ioutils;
@Before
public void setUp() throws Exception {
ioutils = new WrapperUtil();
}
@Test
public void testGetUrlContent() throws IOException {
WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
Mockito.doReturn("test").when(ioutilsSpy).getURLContent(Mockito.anyString());
assertTrue(ioutils2.getURLContent("test").contains("test"));
}
}
这是行不通的:
@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {
@InjectMocks
WrapperUtil ioutils;
@Before
public void setUp() throws Exception {
ioutils = new WrapperUtil();
}
@Test
public void testGetUrlContent() throws IOException {
WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
Mockito.when(ioutilsSpy).getURLContent(Mockito.anyString()).thenReturn("test");
assertTrue(ioutils2.getURLContent("test").contains("test"));
}
}
如何在不使用 PowerMockito 的情况下完成这项工作并实现代码覆盖?非常感谢你的帮助。
我的两分钱在这里:
- 我什至会更进一步定义一个接口来表示功能
- 另一方面,我不会去 "overboard" 测试包装器实现
要点是:这里只有一小部分 glue 代码。如果您能够 测试 此代码以验证此粘合代码是否有效 - 那么您没问题。
换句话说:避免挂断实现 100% 的覆盖率! Coverage 是一个工具,旨在帮助您实现代码质量。
100% 的覆盖率不会 导致“100% 的代码质量”!
您通过尝试 "do the right thing all the time" 来获得代码质量。
这里"right thing"并不是要争取100%的覆盖率。
我猜想如果不求助于 PowerMock(ito),您将无法实现该目标。避免 PowerMock(ito) 本身就是一件好事——我的建议是:简单地接受你无法达到 100% 的覆盖率 class。
如果有的话,我会花时间尝试 排除 这个 class 从覆盖范围运行。
所以我正在尝试在其中包含静态方法的方法上使用 Mockito。原因是我不能使用 PowerMock,所以我将方法包装在非静态方法下。
public class WrapperUtil {
public String getURLContent(String path) throws IOException{
URL url = new URL(path);
return IOUtils.toString(url);
}
}
现在我以两种不同的方式测试了 WrapperUtil class。一个测试有效,但没有为 WrapperUtil class 提供任何覆盖,另一个是抛出与静态方法相关的空指针异常。
这是有效的,但没有提供任何覆盖范围。
@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {
@InjectMocks
WrapperUtil ioutils;
@Before
public void setUp() throws Exception {
ioutils = new WrapperUtil();
}
@Test
public void testGetUrlContent() throws IOException {
WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
Mockito.doReturn("test").when(ioutilsSpy).getURLContent(Mockito.anyString());
assertTrue(ioutils2.getURLContent("test").contains("test"));
}
}
这是行不通的:
@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {
@InjectMocks
WrapperUtil ioutils;
@Before
public void setUp() throws Exception {
ioutils = new WrapperUtil();
}
@Test
public void testGetUrlContent() throws IOException {
WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
Mockito.when(ioutilsSpy).getURLContent(Mockito.anyString()).thenReturn("test");
assertTrue(ioutils2.getURLContent("test").contains("test"));
}
}
如何在不使用 PowerMockito 的情况下完成这项工作并实现代码覆盖?非常感谢你的帮助。
我的两分钱在这里:
- 我什至会更进一步定义一个接口来表示功能
- 另一方面,我不会去 "overboard" 测试包装器实现
要点是:这里只有一小部分 glue 代码。如果您能够 测试 此代码以验证此粘合代码是否有效 - 那么您没问题。
换句话说:避免挂断实现 100% 的覆盖率! Coverage 是一个工具,旨在帮助您实现代码质量。
100% 的覆盖率不会 导致“100% 的代码质量”!
您通过尝试 "do the right thing all the time" 来获得代码质量。
这里"right thing"并不是要争取100%的覆盖率。
我猜想如果不求助于 PowerMock(ito),您将无法实现该目标。避免 PowerMock(ito) 本身就是一件好事——我的建议是:简单地接受你无法达到 100% 的覆盖率 class。
如果有的话,我会花时间尝试 排除 这个 class 从覆盖范围运行。