Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Last week I wrote about writing JUnit tests with accurate exception matching. That relies on the Hamcrest library to work. Unfortunately the JUnit Maven artefact uses only the hamcrest-core dependency which leads to some compilation errors like this...
 Errors
cannot find symbol
symbol: class Matchers
location: package org.hamcrest
cannot find symbol
symbol: method instanceOf(java.lang.Class<java.lang.IllegalArgumentException>)
cannot find symbol
symbol: method containsString(java.lang.String)


The dependency tree from Maven shows up like this...
 Maven Dependency Tree
[INFO] +- junit:junit:jar:4.12:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test


As expected, but incomplete. After checking the Hamcrest Distributables page it becomes clear that the hamcrest-core is the bare minimum dependency and doesn't include the required matchers and supporting classes. What's required is the hamcrest-library dependency. This is easily resolved, just add this to your pom.xml file...
 pom.xml
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>


The dependency tree now looks like this and all of the compilation errors are fixed!
 Maven Dependency Tree
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- junit:junit:jar:4.12:test




-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.