java 如果抛出 JUnit ExpectedException 之后如何继续测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35833116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to continue test after JUnit ExpectedException if thrown?
提问by Deathtiny
I have set up some JUnit (4.12) test with the ExpectedException feature, and I would like the test to continue after the expected exception. But I never see the log '3', as the execution seems to stop after the exception, event if catch?
我已经使用 ExpectedException 功能设置了一些 JUnit (4.12) 测试,并且我希望测试在预期异常之后继续。但我从来没有看到日志“3”,因为执行似乎在异常之后停止,事件是否捕获?
Is this actually possible, and how?
这真的可能吗,怎么可能?
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testUserAlreadyExists() throws Exception {
log.info("1");
// Create some users
userService.createUser("toto1");
userService.createUser("toto2");
userService.createUser("toto3");
Assert.assertTrue( userService.userExists("toto1") );
Assert.assertTrue( userService.userExists("toto2") );
Assert.assertTrue( userService.userExists("toto3") );
log.info("2");
// Try to create an existing user
exception.expect(AlreadyExistsException.class);
userService.createUser("toto1");
log.info("3");
}
采纳答案by Deathtiny
You cannot do that, when the exception is thrown it's thrown for real, ExpectedException
rule or not.
你不能这样做,当抛出异常时,它是真实的,ExpectedException
规则与否。
If you really want this kind of behaviour, you can go back to the "old school" pattern:
如果你真的想要这种行为,你可以回到“老派”模式:
try {
userService.createUser("toto1");
Assert.fail("expecting some AlreadyExistsException here")
} catch (AlreadyExistsException e) {
// ignore
}
log.info("3");
But I wouldn't bother for some log.
但我不会为一些日志而烦恼。
回答by Victor Silva
This SO solution seems to do what you want to do: JUnit continue to assert things after expected exception
这个 SO 解决方案似乎做你想做的事:JUnit 在预期异常后继续断言
I myself was thinking something similar. To continue with the test, you would have to catch the exception yourself in the test. This solution shows an elegant way of doing that.
我自己也在想类似的事情。要继续测试,您必须自己在测试中捕获异常。这个解决方案展示了一种优雅的方式来做到这一点。
Note: If you make a rule to expect an exception (as you did), the test will return successful as soon as that exception is thrown. Reference: http://junit.org/javadoc/latest/org/junit/rules/ExpectedException.html
注意:如果你制定了一个期望异常的规则(就像你所做的那样),一旦抛出该异常,测试就会返回成功。参考:http: //junit.org/javadoc/latest/org/junit/rules/ExpectedException.html
回答by Vladyslav Bezuhlyi
If you don't want to add a lot of similar test methods for something that has many options to throw the expected exception and want to verify that it actually throws on allof the desired cases within a single unit-test instead, I'd suggest this (not pretty maybe) helpful schema:
如果您不想为有很多选项来抛出预期异常的东西添加很多类似的测试方法,并想验证它实际上在单个单元测试中抛出了所有所需的情况,我会建议这个(不太可能)有用的模式:
@Test
public void testThatSomethingExpectedlyFails() {
for (int i = 1; i <= 3; i++) {
try {
switch (i) {
case 1: // smth here throws the exception when configuration #1;
case 2: // smth here throws the exception when configuration #2;
case 3: // smth here throws the exception when configuration #3;
}
} catch (ExceptionThatIsExpected expected) {
continue;
} catch (Exception unexpected) {
/* the test must fail when an unexpected exception is thrown */
fail("The test has failed due to an unexpected exception: " + unexpected.getMessage()); // or just re-throw this exception
}
/* the test must fail when a case completes without the expected exception */
fail("No expected exception occurred at case " + i);
}
}
The one could also iterate items (and even execute functions) of some preliminarily prepared list instead of switch-case with hard-coded integers.
还可以迭代一些预先准备好的列表的项目(甚至执行函数),而不是使用硬编码整数的 switch-case。
回答by thepacker
First of all your test doesn't test one thing. It tests "userExists" and "createUser" under different conditions a.k.a. different scenarios. This is called an AssertionRoulette. You wouldn't need a hack to continue to log "3", if you would write tests, that fail fo the right reason.
首先,您的测试不测试一件事。它在不同条件下(即不同场景)测试“userExists”和“createUser”。这称为断言轮盘赌。你不需要黑客来继续记录“3”,如果你要编写测试,因为正确的原因而失败。
If the tests fail for the right reason, you can see the scenario why it fails without doing all the logging stuff. The Junit-Runner does the logging for you already.
如果测试因正确的原因失败,您可以在不执行所有日志记录的情况下查看失败的原因。Junit-Runner 已经为你做了日志记录。
@Test
public void testUserExists_UserCreatedUserNotExistent_expectTrue()
{
// Create some users
userService.createUser("toto1");
// Assert That user exists
Assert.assertTrue( userService.userExists("toto1") );
}
@Test
public void testCreateUser_UserAlreadyCreated_expectAlreadyExistsExceptionIsThrown()
{
// Create some users
userService.createUser("toto1");
// Try to create an existing user
exception.expect(AlreadyExistsException.class);
userService.createUser("toto1");
}