Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import org.junit.Assert.AssertThat;

I am unable to import org.junit.Assert.AssertThat in my program. I am using Ganymede and jUnit 4.8.1.

like image 534
prasan Avatar asked Feb 08 '11 13:02

prasan


People also ask

What does assertThat do in JUnit?

The assertThat is one of the JUnit methods from the Assert object that can be used to check if a specific value match to an expected one. It primarily accepts 2 parameters. First one if the actual value and the second is a matcher object.

Why assertThat is deprecated?

assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert. assertThat defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.


2 Answers

Static Imports

It's org.junit.Assert.assertThat(T, Matcher<T>) and you can import it as a static import:

import static org.junit.Assert.assertThat

now in your client code you can do assertThat(something, ismatched())

Reference: Java Tutorial > The Static Import Statement


Regular Imports

To do it the old-school way, if you import the Assert class like this

import org.junit.Assert

you can call it using Assert.assertThat(something, isMatched())

(The isMatched() method is something that you'd have to implement)


assertThat()

assertThat() was first described in this blog post and has been part of JUnit ever since version 4.4, so make sure you have JUnit version 4.4 or newer on the classpath. Also, make sure that your compiler compliance level is 1.5 or higher:

Set Eclipse compiler compliance level

like image 62
Sean Patrick Floyd Avatar answered Oct 14 '22 12:10

Sean Patrick Floyd


The method is called assertThat (lower a, capital T). And if you import it like that you need to use a static import:

import static org.junit.Assert.assertThat;

But since you don't tell us the error message I can't really tell if that will work for you.

like image 41
Joachim Sauer Avatar answered Oct 14 '22 10:10

Joachim Sauer