JUnit assertion can be 'assertThat()' call
Reports calls to Assert.assertEquals()
, Assert.assertTrue()
, etc. methods which can be migrated to Hamcrest declarative style Assert.assertThat()
calls.
For example:
public class SubstantialTest {
@Test
public void testContents(Collection<String> c, String s) {
Assert.assertTrue(c.contains(s));
Assert.assertEquals(c, s);
Assert.assertNotNull(c);
Assert.assertNull(c);
Assert.assertFalse(c.contains(s));
}
}
A quick-fix is provided to perform the migration:
public class SubstantialTest {
@Test
public void testContents(Collection<String> c, String s) {
assertThat(c, hasItem(o));
assertThat(o, is(c));
assertThat(c, notNullValue());
assertThat(c, nullValue());
assertThat(c, not(hasItem(o)));
}
}
This inspection requires that the Hamcrest library is available on the classpath.
Use the Statically import matcher's methods option to specify if you want the quick-fix to statically import the Hamcrest matcher methods.
Inspection options
Option | Type | Default |
---|---|---|
Statically import matcher's methods | Checkbox | true |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |
Last modified: 13 July 2023