【问题标题】:Why does JUnit test pass when invoked directly but fail when invoked using Maven?为什么直接调用JUnit测试通过但使用Maven调用失败?
【发布时间】:2014-01-14 13:56:01
【问题描述】:

我今天遇到了一个错误,当通过 Maven 运行时测试失败:“mvn test”并在直接通过 jUnit 运行时通过。

这是有问题的代码:

public class TestAssert {

    @Test
    public void test() {
        assert("test" == "test2");
    }

}

上面的代码通过了一个 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)

直接使用jUnit什么时候通过,使用Maven失败?

【问题讨论】:

  • 你试过 "test".equals("test") 吗?
  • @Guito ive 更新的问题应该是 assert("test" == "test2");
  • 我试图从我的 IDE 执行它,但它也失败了:java.lang.AssertionError。你不希望它失败吗? :) - 编辑:junit 的直接作用是什么?
  • 应该都失败了。你检查过 -ea 标志吗?一般来说,最好使用 assertTrue

标签: java unit-testing maven junit


【解决方案1】:

assert 的评估与否取决于您是否使用-ea(启用断言)标志。所以最好的猜测是你直接运行测试时的默认配置启用了-ea,但maven没有。

使用 junit 测试该条件的正确方法是:

assertTrue("test" == "test2");

或:

assertSame("test", "test2");

【讨论】:

    【解决方案2】:

    JUnit 测试在 Eclipse/GGTS 中通过,但在“mvn test”运行时失败,错误信息为:“Caused by: java.lang.AssertionError

    原因可能与前面的答案正好相反。 尝试为surefile禁用enableAssertions,经过2天的挖掘,它对我有用。 (默认是真的!)

    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>
          <configuration>
            <enableAssertions>false</enableAssertions>
          </configuration>

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 2013-11-08
      • 2012-05-08
      • 2012-08-05
      • 2011-03-22
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多