繁体   English   中英

maven 1在运行测试时失败

[英]maven 1 fails when running test

我在应用程序使用maven 1中将junit版本从3.8更改为4.4。为此,我更改了project.xml,现在看起来像这样:

...
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
      <properties>
<scope>test</scope>
  </properties>
</dependency>
...

我使用@Test或@Before等注释添加了一些测试,这些测试在eclipse中运行完美。 当我尝试在控制台中运行“maven test”时,我得到以下输出:

    test:compile:
[junit] Running com.myapp.Class1Test
[junit] Tests run: 6, Failures: 0, Errors: 0, Time elapsed: 0.721 sec
[junit] Running com.myapp.Class2Test
[junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 1.16 sec
[junit] Running com.myapp.Class3Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.538 sec
[junit] [ERROR] Test com.myapp.Class3Test FAILED
[junit] Running com.myapp.Class4Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.565 sec
[junit] [ERROR] Test com.myapp.Class4Test FAILED
[junit] Running com.myapp.Class5Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.565 sec
[junit] [ERROR] Test com.myapp.Class5Test FAILED
[junit] Running com.myapp.Class6Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.691 sec
[junit] [ERROR] Test com.myapp.Class6Test FAILED
[junit] Running com.myapp.Class7Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.549 sec
[junit] [ERROR] Test com.myapp.Class7Test FAILED
[junit] Running com.myapp.Class8Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.708 sec
[junit] [ERROR] Test com.myapp.Class8Test FAILED
[junit] Running com.myapp.Class9Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.643 sec
[junit] [ERROR] Test com.myapp.Class9Test FAILED
[junit] Running com.myapp.Class10Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.668 sec
[junit] [ERROR] Test com.myapp.Class10Test FAILED
[junit] Running com.myapp.Class11Test
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.644 sec
[junit] [ERROR] Test com.myapp.Class11Test FAILED
[echo]
       ==========================================================
         WARNING:  There were test failures!
       ==========================================================

失败的测试文件内部有多个测试。 Maven只是看不到它们。 此外,当我在调试中使用maven时,我看到它加载隐式junit 3.8。

这就是为什么我认为旧的junit jar被使用了。 我只是不知道在哪里或如何检测它。

任何帮助,建议,光线将不胜感激。

试着找出为什么装载junit 3.8。 你可以使用mvn依赖:tree然后根据结果排除它。

我不知道<properties>元素的范围与你使用它的方式有任何意义,除非另一个插件需要它。 看起来你已经尝试强制Maven 2概念进入Maven 1。

您可以尝试设置maven.test.classpath属性以帮助它获取JUnit JAR。 我对这项工作没有任何保证 - 在Maven 1.x中从未支持过JUnit 4,Maven 1.x现在还没有积极开发超过4年。 正如您将看到的,所有其他响应者正在回答仅与Maven 2相关的答案 - 您应该强烈考虑更新您的构建。

尝试使用最新的(虽然它本身可能不会解决你的问题),也使用

mvn -X clean test

并在原始帖子中包含stactrace。

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.2</version>
  <scope>test</scope>
</dependency>

您还可以查看以下产生的输出并排除 junit 3.8 jar

mvn dependency:tree

尝试这个:

Add an adapter in your tests :
    /**
     * @return instance of this as Junit test case
     */
    public static junit.framework.Test suite ()
    {
        return new JUnit4TestAdapter(IntTestSpringCoherency.class);
    }

取自: http//maven.40175.n5.nabble.com/Can-maven-1-x-run-Junit-4-tests-td92773.html

可能的解决方案/解决方法:

public void AClassUnitTest {
public static junit.framework.Test suite ()
    {
        return new JUnit4TestAdapter(AClassUnitTest.class);
    } 


    @Test
    public method_income_expected(){
    ...
    }

}

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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