8

我在 4.10 使用 junit,在 1.3 声明 hamcrest-core,在 1.3 声明 hamcrest-library。我的问题是 hamcrest-library 和 hamcrest-core 嵌入在 junit 4.10 中。junit 4.11呢?

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
4

2 回答 2

8

如果您浏览到search.maven.org,您可以搜索工件并查看它们的依赖关系。如果您正在使用带有 Maven 插件的 Eclipse,您还可以在 POM 编辑器中单击Dependency Hierarchy 。

查看Maven 网站,您可以看到 JUnit 4.11 依赖于 Hamcrest 1.3:

<dependencies>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

Hamcrest 库你必须自己添加。

于 2013-03-27T14:14:00.730 回答
6

JUnit 4.10 和 JUnit 4.11(如下图所示):

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

... 分别随附 hamcrest-core 1.1 和 1.3。您可以通过利用依赖插件的树目标(运行mvn dependency:tree)亲自看到这一点:

$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building testng 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ testng ---
[INFO] testng:testng:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.10:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.483s
[INFO] Finished at: Fri Mar 29 12:07:22 MDT 2013
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------

这听起来很愚蠢,您需要包含适当的 hamcrest-library artefact 以利用 Hamcrest Matchers。希望这会有所帮助...

于 2013-03-29T18:21:59.347 回答