2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
AssertJ contains many assertions methods specific to common types. Both versions will test the same things, but the dedicated one will provide a better error message, simplifying the debugging process.
This rule reports an issue when an assertion can be simplified to a dedicated one.
The array below gives a non-exhaustive list of assertion reported by the rule. Code behaving similarly, or with a negation will also be reported.
2021-06-03 16:43:28 +02:00
[frame=all]
[cols="^1,^1"]
|===
|Original|Dedicated
s|Related to Object|
|``++assertThat(getObject()).isEqualTo(null)++``|``++assertThat(getObject()).isNull()++``
|``++assertThat(getBoolean()).isEqualTo(true)++``|``++assertThat(getBoolean()).isTrue()++``
|``++assertThat(getBoolean()).isEqualTo(false)++``|``++assertThat(getBoolean()).isFalse()++``
|``++assertThat(x.equals(y)).isTrue()++``|``++assertThat(x).isEqualTo(y)++``
|``++assertThat(x == y).isTrue()++``|``++assertThat(x).isSameAs(y)++``
|``++assertThat(x == null).isTrue()++``|``++assertThat(x).isNull()++``
|``++assertThat(x.toString()).isEqualTo(y)++``|``++assertThat(x).hasToString(y)++``
2022-06-30 15:38:04 +09:00
|``++assertThat(x.hashCode()).isEqualTo(y.hashCode())++``|``++assertThat(x).hasSameHashCodeAs(y)++``
2021-06-03 16:43:28 +02:00
|``++assertThat(getObject() instanceof MyClass).isTrue()++``|``++assertThat(getObject()).isInstanceOf(MyClass.class)++``
s|Related to ``++Comparable++``|
|``++assertThat(x.compareTo(y)).isZero()++``|``++assertThat(x).isEqualByComparingTo(y)++``
2022-06-30 15:38:04 +09:00
|``++assertThat(x >= y).isTrue()++``|``++assertThat(x).isGreaterThanOrEqualTo(y)++``
|``++assertThat(x > y).isTrue()++``|``++assertThat(x).isGreaterThan(y)++``
|``++assertThat(x <= y).isTrue()++``|``++assertThat(x).isLessThanOrEqualTo(y)++``
2021-06-03 16:43:28 +02:00
|``++assertThat(x < y).isTrue()++``|``++assertThat(x).isLessThan(y)++``
s|Related to ``++String++``|
|``++assertThat(getString().isEmpty()).isTrue()++``|``++assertThat(getString()).isEmpty()++``
2022-06-30 15:38:04 +09:00
|``++assertThat(getString()).hasSize(0)++``|``++assertThat(getString()).isEmpty()++``
2021-06-03 16:43:28 +02:00
|``++assertThat(getString().equals(expected)).isTrue()++``|``++assertThat(getString()).isEqualTo(expected)++``
|``++assertThat(getString().equalsIgnoreCase(expected)).isTrue()++``|``++assertThat(getString()).isEqualToIgnoringCase(expected)++``
|``++assertThat(getString().contains(expected)).isTrue()++``|``++assertThat(getString()).contains(expected)++``
|``++assertThat(getString().startsWith(expected)).isTrue()++``|``++assertThat(getString()).startsWith(expected)++``
|``++assertThat(getString().endsWith(expected)).isTrue()++``|``++assertThat(getString()).endsWith(expected)++``
|``++assertThat(getString().matches(expected)).isTrue()++``|``++assertThat(getString()).matches(expected)++``
|``++assertThat(getString().trim()).isEmpty()++``|``++assertThat(getString()).isBlank()++``
|``++assertThat(getString().length()).isEqualTo(length)++``|``++assertThat(getString()).hasSize(length)++``
|``++assertThat(getString().length()).hasSize(expected.length())++``|``++assertThat(getString()).hasSameSizeAs(expected)++``
s| Related to ``++File++``|
|``++assertThat(getFile()).hasSize(0)++``|``++assertThat(getFile()).isEmpty()++``
|``++assertThat(getFile().length()).isZero()++``|``++assertThat(getFile()).isEmpty()++``
|``++assertThat(getFile().length()).isEqualTo(length)++``|``++assertThat(getFile()).hasSize(length)++``
|``++assertThat(getFile().canRead()).isTrue()++``|``++assertThat(getFile()).canRead()++``
|``++assertThat(getFile().canWrite()).isTrue()++``|``++assertThat(getFile()).canWrite()++``
|``++assertThat(getFile().exists()).isTrue()++``|``++assertThat(getFile()).exists()++``
|``++assertThat(getFile().getName()).isEqualTo(name)++``|``++assertThat(getFile()).hasName(name)++``
|``++assertThat(getFile().getParent()).isEqualTo(pathname)++``|``++assertThat(getFile()).hasParent(pathname)++``
|``++assertThat(getFile().getParentFile()).isNull()++``|``++assertThat(getFile()).hasNoParent()++``
|``++assertThat(getFile().isAbsolute()).isTrue()++``|``++assertThat(getFile()).isAbsolute()++``
|``++assertThat(getFile().isAbsolute()).isFalse()++``|``++assertThat(getFile()).isRelative()++``
|``++assertThat(getFile().isDirectory()).isTrue()++``|``++assertThat(getFile()).isDirectory()++``
|``++assertThat(getFile().isFile()).isTrue()++``|``++assertThat(getFile()).isFile()++``
|``++assertThat(getFile().list()).isEmpty()++``|``++assertThat(getFile()).isEmptyDirectory()++``
s| Related to ``++Path++``|
|``++assertThat(getPath().startsWith(path)).isTrue()++``|``++assertThat(getPath()).startsWithRaw(path)++``
|``++assertThat(getPath().endsWith(path)).isTrue()++``|``++assertThat(getPath()).endsWithRaw(path)++``
|``++assertThat(getPath().getParent()).isEqualTo(name)++``|``++assertThat(getPath()).hasParentRaw(name)++``
|``++assertThat(getPath().getParent()).isNull()++``|``++assertThat(getPath()).hasNoParentRaw()++``
|``++assertThat(getPath().isAbsolute()).isTrue()++``|``++assertThat(getPath()).isAbsolute()++``
|``++assertThat(getPath().isAbsolute()).isFalse()++``|``++assertThat(getPath()).isRelative()++``
s| Related to ``++Array++``|
|``++assertThat(getArray().length).isZero()++``|``++assertThat(getArray()).isEmpty()++``
|``++assertThat(getArray().length).isEqualTo(length)++``|``++assertThat(getArray()).hasSize(length)++``
2022-06-30 15:38:04 +09:00
|``++assertThat(getArray().length).isEqualTo(anotherArray.length)++``|``++assertThat(getArray()).hasSameSizeAs(anotherArray)++``
2021-06-03 16:43:28 +02:00
|``++assertThat(getArray().length).isLessThanOrEqualTo(expression)++``|``++assertThat(getArray()).hasSizeLessThanOrEqualTo(expression)++``
|``++assertThat(getArray().length).isLessThan(expression)++``|``++assertThat(getArray()).hasSizeLessThan(expression)++``
|``++assertThat(getArray().length).isGreaterThan(expression)++``|``++assertThat(getArray()).hasSizeGreaterThan(expression)++``
|``++assertThat(getArray().length).isGreaterThanOrEqualTo(expression)++``|``++assertThat(getArray()).hasSizeGreaterThanOrEqualTo(expression)++``
s|Related to ``++Collection++``|
|``++assertThat(getCollection().isEmpty()).isTrue()++``|``++assertThat(getCollection()).isEmpty()++``
|``++assertThat(getCollection().size()).isZero()++``|``++assertThat(getCollection()).isEmpty()++``
|``++assertThat(getCollection().contains(something)).isTrue()++``|``++assertThat(getCollection()).contains(something)++``
|``++assertThat(getCollection().containsAll(otherCollection)).isTrue()++``|``++assertThat(getCollection()).containsAll(otherCollection)++``
s|Related to ``++Map++``|
|``++assertThat(getMap().size()).isEqualTo(otherMap().size()++``|``++assertThat(getMap()).hasSameSizeAs(otherMap())++``
|``++assertThat(getMap().containsKey(key)).isTrue()++``|``++assertThat(getMap()).containsKey(key)++``
|``++assertThat(getMap().keySet()).contains(key)++``|``++assertThat(getMap()).containsKey(key)++``
|``++assertThat(getMap().keySet()).containsOnlyKey(key)++``|``++assertThat(getMap()).containsOnlyKey(key)++``
|``++assertThat(getMap().containsValue(value)).isTrue()++``|``++assertThat(getMap()).containsValue(value)++``
2022-06-30 15:38:04 +09:00
|``++assertThat(getMap().values()).contains(value)++``|``++assertThat(getMap()).containsValue(value)++``
2021-06-03 16:43:28 +02:00
|``++assertThat(getMap().get(key)).isEqualTo(value)++``|``++assertThat(getMap()).containsEntry(key, value)++``
s|Related to ``++Optional++``|
|``++assertThat(getOptional().isPresent()).isTrue()++``|``++assertThat(getOptional()).isPresent()++``
|``++assertThat(getOptional().get()).isEqualTo(something)++``|``++assertThat(getOptional()).contains(something)++``
|``++assertThat(getOptional().get()).isSameAs(something)++``|``++assertThat(getOptional()).containsSame(something)++``
|===
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
assertThat(getObject()).isEqualTo(null); // Noncompliant
assertThat(getObject()).isNotEqualTo(null); // Noncompliant - not listed above but also supported
assertThat(getString().trim()).isEmpty();
assertThat(getFile().canRead()).isTrue();
assertThat(getPath().getParent()).isNull();
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
assertThat(getObject()).isNull();
2022-06-30 15:38:04 +09:00
assertThat(getObject()).isNotNull();
2021-04-28 16:49:39 +02:00
assertThat(getString()).isBlank();
assertThat(getFile()).canRead();
assertThat(getPath()).hasNoParentRaw();
----
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Use XXX instead.
=== Highlighting
assertThat(expression).method()
* Primary: method
* Secondary: expression
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]