在Maven中,您可以这样声明JUnit依赖项:
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
但是,它附带了hamcrest-core
库的捆绑副本。
$ mvn dependency:tree
...
[INFO] \- junit:junit:jar:4.12:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
...
1. Maven + JUnit + Hamcrest
注意
使用hamcrest-core
的默认JUnit捆绑副本不是一个好主意,最好将其排除。
再次查看更新的pom.xml
,它不包含hamcrest-core
的JUnit捆绑副本。 另一方面,它还包括有用的hamcrest-library
:
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- This will get hamcrest-core automatically -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
再次查看依赖关系树。
$ mvn dependency:tree
...
[INFO] +- junit:junit:jar:4.12:test
[INFO] \- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
...