简体   繁体   中英

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.

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 :

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 ?

assert is evaluated or not depending on whether you use the -ea (enable assert) flag. So the best guess is that your default configuration when running the test directly has -ea enabled but maven doesn't.

The proper way to test that condition with junit is:

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 "

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. (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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM