简体   繁体   English

为什么直接调用JUnit测试会通过,但是使用Maven调用时会失败?

[英]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.

相关问题 CDI:从JUnit测试调用时,不会调用Interceptor - CDI: Interceptor is not invoked when called from a JUnit test getView 何时完成调用? - When does the getView finished invoked? 为什么 junit 测试在 eclipse 中失败,但从 ant 中通过? - Why does a junit test fail in eclipse but pass from ant? java:NullPointerExc为什么当直接在其上调用lambda表达式时List包含添加的对象。 而不是当lambda是一种方法时 - java: NullPointerExc why a List contains the added object when a lambda expression is invoked directly on it. and Not when the lambda is a method 测试抛出异常时调用的方法 - Method invoked when a test throws an Exception Junit 测试通过,即使内部调用的方法可能失败 - Junit Test Pass even when the method called inside may fail 调用exec-maven-plugin会打破默认的maven生命周期 - exec-maven-plugin when invoked breaks the default maven lifecycle 为什么/何时调用ComponentListener.componentShown()? - Why/When ComponentListener.componentShown() get invoked? 为什么调用线程时JFrame无法显示? - Why JFrame fails to display when thread is invoked? 为什么在验证阶段调用 jfrog maven artifactory 插件? - Why does jfrog maven artifactory plugin is invoked at validation phase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM