【问题标题】:junit and hamcrest declarationjunit 和 hamcrest 宣言
【发布时间】:2013-03-27 14:02:25
【问题描述】:

我在 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>

【问题讨论】:

    标签: maven junit hamcrest


    【解决方案1】:

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

    查看Maven website,您可以看到 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 库。

    【讨论】:

    • 使用junit 4.11时是否需要明确添加hamcrest-core 1.3依赖?当我用谷歌搜索它时,它说“JUnit 4.11 不再包含 org.hamcrest 类。”两种说法都是矛盾的。
    • 将 hamcrest 作为依赖项并包含 hamcrest 类是两件不同的事情。 “包括类”意味着这些类在 junit.jar 中。 “作为依赖项”意味着您需要 hamcrest jar 来构建/运行 junit。 (如果您使用的是 maven:依赖项将自动神奇地包含在您的类路径中。如果您不使用 maven,则必须在类路径中手动添加依赖项)
    • @ben75 是对的。它们意味着 jar in 中不再有 Hamcrest 类。但是,这取决于它们是否在另一个罐子中,这取决于它。
    • 使用 JUnit 4.11,当我从 pom.xml 中删除对 hamcrest-all 的依赖项时,我的测试中会出现编译错误。我假设hamcrest-core 仅包含与 Hamcrest 交互所需的基本类,包括MatcherMatcherAssertAssertionError。您需要其他 JAR 来获得可用的匹配器。
    • @Alex 我希望范围是“测试”,不是这样吗?
    【解决方案2】:

    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 库人工制品才能利用 Hamcrest 匹配器。希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多