简体   繁体   English

如何使单元测试在Maven Tycho构建中运行?

[英]How to get unit tests to run in Maven Tycho build?

I've done a lot of work in the past writing unit tests that run in "conventional" Maven builds, using JUnit and Mockito (and PowerMock). 过去,我已经做了很多工作,编写了使用JUnit和Mockito(以及PowerMock)在“常规” Maven构建中运行的单元测试 I'm now working on an Eclipse plugin codebase, which builds with Maven Tycho. 我现在正在使用Maven Tycho构建的Eclipse插件代码库。

Overall, it's a multiproject build, but I'm only adding unit tests to one of the plugin projects (for now). 总的来说,这是一个多项目构建,但我只是将单元测试添加到其中一个插件项目中(目前)。

I've heard of tycho-surefire , but that seems pretty complicated, and it really sounds more like it supports integration tests instead of unit tests . 我听说过tycho-surefire ,但这似乎很复杂,听起来确实更像是支持集成测试而不是单元测试 I'm guessing I'll probably have no choice but to use this, but so far I haven't tried to integrate it. 我猜想我可能别无选择,只能使用它,但是到目前为止,我还没有尝试将其集成。

I tried getting the JUnit and Mockito artifacts from Maven, and then using the maven-dependency-plugin to get the artifacts available to be referenced in the Bundle-Classpath property of the manifest. 我尝试从Maven获取JUnit和Mockito工件,然后使用maven-dependency-plugin获取可在清单的Bundle-Classpath属性中引用的工件。

When I run the build, the tycho-compiler-plugin I see it compiling 105 source files, which includes all of the classes in src/main/java and src/test/java . 当我运行构建时,我看到tycho-compiler-plugin 编译了105个源文件,其中包括src/main/javasrc/test/java It fails to compile the test class because it can't find the Mockito classes, even though when I run the build with -X , it shows the mockito-all artifact in the dependency tree. 它无法compile测试类,因为它找不到Mockito类,即使当我使用-X运行构建时,它也会在依赖项树中显示“ mockito-all构件。

What can I do here? 我在这里可以做什么?

After a lot of painful Maven trial & error I struggled across this website , which provides a surprisingly easy way to use unit-tests in a Maven-Tycho setup. 经过大量痛苦的Maven试验和错误之后,我在该网站上苦苦挣扎, 该网站提供了一种在Maven-Tycho设置中使用单元测试的简单方法。

Here, the important parts of the pom.xml when using JUnit (probably looks similar for Mockito): 在这里,使用JUnit时pom.xml的重要部分(对于Mockito可能看起来很相似):

<testSourceDirectory>src/test/java</testSourceDirectory>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <executions>
      <execution>
        <id>test</id>
        <phase>test</phase>
        <configuration>
          <includes>
            <include>**/*Test.java</include>
          </includes>
        </configuration>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <id>compiletests</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

Name all your tests in a way so that they end with *Test.java . 以某种方式命名所有测试,使其以*Test.java结尾。 Run mvn test in order to execute all available unit-tests. 运行mvn test以执行所有可用的单元测试。

You have to use junit and Mockito as osgi bundles 您必须将junit和Mockito用作osgi捆绑包

I think this Question answered detailed here 我认为这个问题在这里详细回答

I hope this helps. 我希望这有帮助。

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

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