# Replace JUnit assertThat with Hamcrest
# Description
The JUnit Assert.assertThat
(opens new window) method is deprecated. Its sole purpose is to forward the call to the MatcherAssert.assertThat
(opens new window) method defined in Hamcrest 1.3.
Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.
This rule finds the deprecated usages of Assert.assertThat
and automatically replaces them with MatcherAssert.assertThat
.
Since JUnit 5 contains no equivalent assertion for assertThat
, this rule eliminates an obstacle for migration to JUnit 5.
Requirements
This rule requires the following library to be present:
- org.hamcrest:hamcrest-core:1.3
# Benefits
Removes deprecated code. Helps migrating to JUnit 5.
# Tags
# Code Changes
# Simple assertThat
Invocation
Pre
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
assertThat(user, equalTo(new User("John", "Snow")));
}
}
Post
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
assertThat(user, equalTo(new User("John", "Snow")));
}
}
# assertThat
with Reason
Pre
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
assertThat("Expecting to find user John with id '0'", user, equalTo(new User("John", "Snow")));
}
}
Post
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
assertThat("Expecting to find user John with id '0'", user, equalTo(new User("John", "Snow")));
}
}
# Replacing assertThat
Qualifier
Pre
import static org.hamcrest.Matchers.equalTo;
import org.junit.Assert;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
Assert.assertThat(user, equalTo(new User("John", "Snow")));
}
}
Post
import static org.hamcrest.Matchers.equalTo;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
public class UserRepositoryTest {
@Test
public void replacingAssertThat() {
User user = userRepo.findById("0");
MatcherAssert.assertThat(user, equalTo(new User("John", "Snow")));
}
}
Use a Java Refactoring Tool
No license required
You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.
System-wide Refactoring
Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.
# Properties
Property | Value |
---|---|
Rule ID | ReplaceJUnitAssertThatWithHamcrest |
First seen in jSparrow version | 3.29.0 |
Minimum Java version | 5 |
Remediation cost | 2 min |