7

The import org.hamcrest.Matchers.hasProperty cannot be resolved in JUnit4.12.

What is the alternative to use hasProperty?

glytching
  • 44,936
  • 9
  • 114
  • 120
krish
  • 95
  • 1
  • 5

3 Answers3

5

Hamcrest is not embedded in JUnit 4.12, instead you'll need to include the separate Hamcrest library on your classpath.

If you are using Maven you can do this by including the following dependency in your pom.xml:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency> 

Alternatively you can download the JAR from Maven Central.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • 1
    FWIW, In 2021 *hamcrest-library* has been replaced by the *hamcrest* package as follows (using Gradle notation); `testImplementation org.hamcrest:hamcrest:2.2` – dbaltor May 19 '21 at 14:48
3

In case you only need it for UnitTests you can use following dependency (works with JUnit5 as well):

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-junit</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>
SWiggels
  • 2,159
  • 1
  • 21
  • 35
0

Use this import if you are not able to use the hamcrest "equalTo" method.

import static org.hamcrest.Matchers.equalTo;

body("scope", equalTo("APP"));