[英]How to throw exception using PowerMock/PowerMockito/Mockito when a call to void method is made?
[英]Powermock/mockito does not throw exception when told to
我本以为应该通过以下测试,但是永远不会抛出异常。 有什么线索吗?
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticService.class)
public class TestStuff {
@Test(expected = IllegalArgumentException.class)
public void testMockStatic() throws Exception {
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(StaticService.say("hello"));
verifyStatic();
StaticService.say("hello");
}
}
这是因为在静态方法中语法错误地使用了doThrow...。 我在下面的两种独立的测试方法中概述了几种解决此问题的方法。
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticService.class})
public class StaticServiceTest {
@Test (expected = IllegalArgumentException.class)
public void testMockStatic1() throws Exception {
String sayString = "hello";
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class, "say", Matchers.eq(sayString));
StaticService.say(sayString);
fail("Expected exception not thrown.");
}
@Test (expected = IllegalArgumentException.class)
public void testMockStatic2() throws Exception {
String sayString = "hello";
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class);
StaticService.say(Matchers.eq(sayString)); //Using the Matchers.eq() is
//optional in this case.
//Do NOT use verifyStatic() here. The method hasn't actually been
//invoked yet; you've only established the mock behavior. You shouldn't
//need to "verify" in this case anyway.
StaticService.say(sayString);
fail("Expected exception not thrown.");
}
}
作为参考,这是我创建的StaticService实现。 我不知道它是否与您的匹配,但我确实确认测试通过了。
public class StaticService {
public static void say(String arg) {
System.out.println("Say: " + arg);
}
}
带有导入的StaticServiceTest.java :
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ StaticService.class })
public class StaticServiceTest {
@Test(expected = IllegalArgumentException.class)
public void testMockStatic1() throws Exception {
/* Setup */
String sayString = "hello";
PowerMockito.mockStatic(StaticService.class);
/* Mocks */
PowerMockito.doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class, "say", Matchers.eq(sayString));
/* Test */
StaticService.say(sayString);
/* Asserts */
fail("Expected exception not thrown.");
}
@Test(expected = IllegalArgumentException.class)
public void testMockStatic2() throws Exception {
/* Setup */
String sayString = "hello";
PowerMockito.mockStatic(StaticService.class);
/* Mocks */
PowerMockito.doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class);
StaticService.say(Matchers.eq(sayString));
/* Test */
StaticService.say(sayString);
/* Asserts */
fail("Expected exception not thrown.");
}
}
StaticService.java
public class StaticService {
public static void say(String arg) {
System.out.println("Say: " + arg);
}
}
测试通过:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.