无法使用 PowerMockito 模拟系统 class 静态方法
Unable to mock System class static method using PowerMockito
即使我已经阅读了手册并通过了 Powermock 的多个答案,也无法为我的用例模拟静态方法。
Class:
@Component
public class SCUtil{
public void createSC(){
try {
String host = InetAddress.getLocalHost().getHostAddress();
// ...
// ...
// ...
} catch (UnknownHostException e) {
log.error("Exception in creasting SC");
throw new ServiceException(e);
}
}
}
测试class:
@RunWith(PowerMockRunner.class)
@PrepareForTest( InetAddress.class )
public class SCUtilTest {
@InjectMocks
private SCUtil scUtil;
private Event event;
@Before
public void beforeEveryTest () {
event = new InterventionEvent();
}
@Test(expected = ServiceException.class)
public void testCreateSC_Exception () {
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException("test"));
scUtil.createSC(event);
}
}
在这里,测试失败,因为没有抛出异常:
java.lang.AssertionError: Expected exception:
com.example.v1.test.selftest.errorhandling.ServiceException
我已经为此花费了几个多小时,但仍然无法正常工作。我做错了什么?
提前感谢大家的帮助:)
java.net.InetAddress
是一个系统class。系统class的调用者应该定义在@PrepareForTest({ClassThatCallsTheSystemClass.class})
中。
参见 documentation。
The way to go about mocking system classes are a bit different than
usual though. Normally you would prepare the class that contains the
static methods (let's call it X) you like to mock but because it's
impossible for PowerMock to prepare a system class for testing so
another approach has to be taken. So instead of preparing X you
prepare the class that calls the static methods in X!
请注意 @InjectMocks
注释不会注入静态模拟,它可以被删除。
工作测试示例:
@RunWith(PowerMockRunner.class)
@PrepareForTest(SCUtil.class)
public class SCUtilTest {
private SCUtil scUtil = new SCUtil();
@Test(expected = ServiceException.class)
public void testCreateSC_Exception () throws UnknownHostException {
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException("test"));
scUtil.createSC();
}
}
即使我已经阅读了手册并通过了 Powermock 的多个答案,也无法为我的用例模拟静态方法。
Class:
@Component
public class SCUtil{
public void createSC(){
try {
String host = InetAddress.getLocalHost().getHostAddress();
// ...
// ...
// ...
} catch (UnknownHostException e) {
log.error("Exception in creasting SC");
throw new ServiceException(e);
}
}
}
测试class:
@RunWith(PowerMockRunner.class)
@PrepareForTest( InetAddress.class )
public class SCUtilTest {
@InjectMocks
private SCUtil scUtil;
private Event event;
@Before
public void beforeEveryTest () {
event = new InterventionEvent();
}
@Test(expected = ServiceException.class)
public void testCreateSC_Exception () {
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException("test"));
scUtil.createSC(event);
}
}
在这里,测试失败,因为没有抛出异常:
java.lang.AssertionError: Expected exception: com.example.v1.test.selftest.errorhandling.ServiceException
我已经为此花费了几个多小时,但仍然无法正常工作。我做错了什么?
提前感谢大家的帮助:)
java.net.InetAddress
是一个系统class。系统class的调用者应该定义在@PrepareForTest({ClassThatCallsTheSystemClass.class})
中。
参见 documentation。
The way to go about mocking system classes are a bit different than usual though. Normally you would prepare the class that contains the static methods (let's call it X) you like to mock but because it's impossible for PowerMock to prepare a system class for testing so another approach has to be taken. So instead of preparing X you prepare the class that calls the static methods in X!
请注意 @InjectMocks
注释不会注入静态模拟,它可以被删除。
工作测试示例:
@RunWith(PowerMockRunner.class)
@PrepareForTest(SCUtil.class)
public class SCUtilTest {
private SCUtil scUtil = new SCUtil();
@Test(expected = ServiceException.class)
public void testCreateSC_Exception () throws UnknownHostException {
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException("test"));
scUtil.createSC();
}
}