[英]Why does JUnit test pass when invoked directly but fail when invoked using Maven?
I encountered a bug today where a test was failing when run via Maven : "mvn test" and passing when running directly via jUnit. 我今天遇到一个错误,该错误通过Maven运行时测试失败:“ mvn测试”,而直接通过jUnit运行时通过。
Here is code in question : 这是有问题的代码:
public class TestAssert {
@Test
public void test() {
assert("test" == "test2");
}
}
The above code passes a Junit test but when the test is executed using Maven I received this error : 上面的代码通过了Junit测试,但是当使用Maven执行测试时,我收到此错误:
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec <<< FAILURE!
test(TestAssert) Time elapsed: 0.003 sec <<< FAILURE!
java.lang.AssertionError
at TestAssert.test(TestAssert.java:15)
When does it pass when using jUnit directly and fail when using Maven ? 直接使用jUnit时何时通过而使用Maven时失败?
assert
is evaluated or not depending on whether you use the -ea
(enable assert) flag. assert
这取决于您是否使用评估或不-ea
(启用断言)标志。 So the best guess is that your default configuration when running the test directly has -ea
enabled but maven doesn't. 因此,最好的猜测是,直接运行测试时的默认配置已启用
-ea
但未启用maven。
The proper way to test that condition with junit is: 用junit测试该条件的正确方法是:
assertTrue("test" == "test2");
or: 要么:
assertSame("test", "test2");
JUnit tests passed in Eclipse/GGTS, but failed when running by "mvn test" with the error info: " Caused by: java.lang.AssertionError " JUnit测试在Eclipse / GGTS中通过,但在通过“ mvn test”运行时失败,并显示错误信息:“ 引起者:java.lang.AssertionError ”
The cause could be just the opposite with the answer earlier here. 原因可能与此处前面的答案恰恰相反。 Try disabling enableAssertions for surefile and it works for me after 2 days' digging.
尝试为surefile禁用enableAssertions,并且在挖掘2天后对我有用。 (it's true by default!)
(默认情况下是这样!)
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <enableAssertions>false</enableAssertions> </configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.