Fix for multiple JUnit Conversions in batch mode with and without delta creating an exception.

Added new AssertThatObjectExpression inspection for toString() and hashCode() and moved equals() from AssertThatBinaryExpression there.
This commit is contained in:
Chris Hodges 2019-06-09 21:35:23 +02:00
parent 6795622202
commit 5c455c3ca9
19 changed files with 235 additions and 53 deletions

View File

@ -171,6 +171,21 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
```
Analogously with ```isFalse()```.
- AssertThatObjectExpression
Handles equals(), toString() and hashCode() inside an expected expression.
```
from: assertThat(objActual.equals(objExpected)).isTrue();
to: assertThat(objActual).isEqualTo(objExpected);
from: assertThat(objActual.toString()).isEqualTo(stringExpected);
to: assertThat(objActual).hasToString(stringExpected);
from: assertThat(objActual.hashCode()).isEqualTo(objExpected.hashCode());
to: assertThat(objActual).hasSameHashCodeAs(objExpected);
```
- AssertThatCollectionOrMapExpression
Moves collection and map operations inside ```assertThat()``` out.
@ -273,9 +288,6 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
from: assertThat(null == objActual).isFalse();
to: assertThat(objActual).isNotNull();
from: assertThat(objActual.equals(objExpected).isTrue();
to: assertThat(objActual).isEqualTo(objExpected);
```
...and many, many more combinations (more than 150).
@ -494,9 +506,9 @@ Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for yo
## Planned features
- Joining .contains() expressions
- Removing .isPresent().contains() combinations for Optionals
- Converting ```foo.compareTo(bar) == 0``` to ```isEqualTo()``` (yes, I've *really* seen code like that)
- Extraction with property names to lambda with Java 8
```
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
to: assertThat(object).extracting(type::getPropOne, it -> it.propNoGetter, it -> it.getPropTwo().getInnerProp())...
@ -504,13 +516,15 @@ Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for yo
## Changelog
#### V1.1 (unreleased)
#### V1.1 (09-Jun-19)
- Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection.
- Added Guava Optional ```opt.orNull() == null``` case. You know, I'm not making this stuff up, people actually write this kind of code.
- Added Java 8 Optional ```opt.orElse(null) == null``` case, too.
- Extended JUnitAssertToAssertJ inspection to convert JUnit ```assume```-Statements, too.
- Extended JUnitAssertToAssertJ inspection to convert JUnit ```assume``` statements, too.
- Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
- New ImplicitAssertion inspection for implicit ```isNotNull()```, ```isNotEmpty()``` and ```isPresent()``` assertions that will be covered by chained assertions.
- Fix for multiple JUnit Conversions in batch mode with and without delta creating an exception.
- Added new AssertThatObjectExpression inspection for ```toString()``` and ```hashCode()``` and moved ```equals()``` from AssertThatBinaryExpression there.
#### V1.0 (06-May-19)
- First release to be considered stable enough for production use.

View File

@ -42,14 +42,16 @@ intellij {
patchPluginXml {
changeNotes """
<h4>V1.1 (unreleased)</h4>
<h4>V1.1 (09-Jun-19)</h4>
<ul>
<li>Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection.
<li>Added Guava Optional opt.orNull() == null case. You know, I'm not making this stuff up, people actually write this kind of code.
<li>Added Java 8 Optional opt.orElse(null) == null case, too.
<li>Extended JUnitAssertToAssertJ inspection to convert JUnit assume-Statements, too.
<li>Extended JUnitAssertToAssertJ inspection to convert JUnit assume statements, too.
<li>Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
<li>New ImplicitAssertion inspection for implicit isNotNull(), isNotEmpty() and isPresent() assertions that will be covered by chained assertions.
<li>Fix for multiple JUnit Conversions in batch mode with and without delta creating an exception.
<li>Added new AssertThatObjectExpression inspection for toString() and hashCode() and moved equals() from AssertThatBinaryExpression there.
</ul>
<p>Full changelog available at <a href="https://github.com/chrisly42/cajon-plugin#changelog">Github project site</a>.</p>
"""

View File

@ -5,15 +5,14 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a binary expression"
private const val SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE = "Split %s expression out of assertThat()"
private const val MORE_MEANINGFUL_MESSAGE_TEMPLATE = "Moving %s expression out of assertThat() would be more meaningful"
private const val SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE = "Split binary expression out of assertThat()"
private const val MORE_MEANINGFUL_MESSAGE_TEMPLATE = "Moving binary expression out of assertThat() would be more meaningful"
}
override fun getDisplayName() = DISPLAY_NAME
@ -33,16 +32,7 @@ class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
val expectedResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: return
val assertThatArgument = staticMethodCall.firstArg
if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) {
val replacementMethod = expectedResult.map(MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO)
registerSplitMethod(holder, expectedCallExpression, "${MethodNames.EQUALS}()", replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method)
}
return
}
val binaryExpression = assertThatArgument as? PsiBinaryExpression ?: return
val binaryExpression = staticMethodCall.firstArg as? PsiBinaryExpression ?: return
val leftType = binaryExpression.lOperand.type ?: return
val rightType = binaryExpression.rOperand?.type ?: return
@ -53,7 +43,7 @@ class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
return
} else if (isLeftNull || isRightNull) {
val replacementMethod = expectedResult.map(MethodNames.IS_NULL, MethodNames.IS_NOT_NULL)
registerSplitMethod(holder, expectedCallExpression, "binary", replacementMethod) { desc, method ->
registerSplitMethod(holder, expectedCallExpression, replacementMethod) { desc, method ->
SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = isLeftNull, noExpectedExpression = true)
}
return
@ -75,7 +65,7 @@ class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
(isPrimitive || isNumericType).map(TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP, TOKEN_TO_ASSERTJ_FOR_OBJECT_MAPPINGS)
val replacementMethod = mappingToUse[tokenType] ?: return
registerSplitMethod(holder, expectedCallExpression, "binary", replacementMethod) { desc, method ->
registerSplitMethod(holder, expectedCallExpression, replacementMethod) { desc, method ->
SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = swapExpectedAndActual)
}
}
@ -85,13 +75,10 @@ class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
private fun registerSplitMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
type: String,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val description = SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE.format(type)
val message = MORE_MEANINGFUL_MESSAGE_TEMPLATE.format(type)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, message, quickfix)
val quickfix = quickFixSupplier(SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE, replacementMethod)
holder.registerProblem(expression, MORE_MEANINGFUL_MESSAGE_TEMPLATE, quickfix)
}
}

View File

@ -34,11 +34,6 @@ class AssertThatCollectionOrMapExpressionInspection : AbstractAssertJInspection(
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "containsValue").parameterCount(1),
MethodNames.CONTAINS_VALUE, MethodNames.DOES_NOT_CONTAIN_VALUE
),
// TODO not quite a collection or map expression, maybe move this out to new inspection together with equals and hashcode.
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "toString").parameterCount(0),
MethodNames.HAS_TO_STRING, null
)
)
}

View File

@ -0,0 +1,59 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.HasHashCodeQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.RemoveActualOutmostMethodCallQuickFix
class AssertThatObjectExpressionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting equals(), toString(), or hashCode()"
private const val HASHCODE_MESSAGE_TEMPLATE = "Replacing hashCode() expressions by hasSameHashCode() would be more concise"
private val OBJECT_TO_STRING = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "toString").parameterCount(0)
private val OBJECT_HASHCODE = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "hashCode").parameterCount(0)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) {
return
}
val staticMethodCall = statement.findStaticMethodCall() ?: return
val assertThatArgument = staticMethodCall.firstArg as? PsiMethodCallExpression ?: return
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
when {
OBJECT_EQUALS.test(assertThatArgument) -> {
val expectedResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: return
val replacementMethod = expectedResult.map(MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO)
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method)
}
}
OBJECT_TO_STRING.test(assertThatArgument) -> {
staticMethodCall.findFluentCallTo(IS_EQUAL_TO_OBJECT) ?: return
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.HAS_TO_STRING) { desc, method ->
RemoveActualOutmostMethodCallQuickFix(desc, method)
}
}
OBJECT_HASHCODE.test(assertThatArgument) -> {
val isEqualTo = staticMethodCall.findFluentCallTo(IS_EQUAL_TO_INT) ?: return
val expectedExpression = isEqualTo.firstArg as? PsiMethodCallExpression ?: return
if (OBJECT_HASHCODE.test(expectedExpression)) {
holder.registerProblem(expectedCallExpression, HASHCODE_MESSAGE_TEMPLATE, HasHashCodeQuickFix())
}
}
}
}
}
}
}

View File

@ -115,8 +115,7 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
if (!expression.hasAssertThat()) {
return
}
val isHasSize = HAS_SIZE.test(expression)
if (!(isHasSize)) {
if (!HAS_SIZE.test(expression)) {
return
}
val actualExpression = expression.firstArg

View File

@ -0,0 +1,37 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class HasHashCodeQuickFix :
AbstractCommonQuickFix(HASHCODE_DESCRIPTION) {
companion object {
private const val HASHCODE_DESCRIPTION = "Replace calls to hashCode() with hasSameHashCodeAs()"
}
override fun getFamilyName(): String {
return HASHCODE_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
val methodsToFix = assertThatMethodCall.gatherAssertionCalls()
assertExpression.replace(assertExpression.qualifierExpression)
methodsToFix
.forEach {
val innerHashCodeObject = (it.firstArg as PsiMethodCallExpression).qualifierExpression
val expectedExpression = createExpectedMethodCall(it, "hasSameHashCodeAs", innerHashCodeObject)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}

View File

@ -9,7 +9,7 @@ import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.GUAVA_ASSE
class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
companion object {
private const val CONVERT_DESCRIPTION = "Convert JUnit assertions to assertJ"
private const val CONVERT_DESCRIPTION = "Convert JUnit assertions with delta to assertJ"
}
override fun getFamilyName(): String {

View File

@ -40,6 +40,8 @@
<localInspection groupPath="Java" shortName="AssertThatBinaryExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatObjectExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatStringExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatCollectionOrMapExpression" enabledByDefault="true" level="WARNING"

View File

@ -4,7 +4,6 @@ Turns a binary expression in the kind of assertThat(actual &lt;operator&gt; expe
into assertThat(actual).is&lt;operator&gt;(expected).
<!-- tooltip end -->
There are over 150 combinations that are found with this inspections.
It also detects assertThat(actual.equals(expected)) assertions.
Also works with constant expressions on the expected side.
Swaps actual and expected when actual is a constant expression (correctly transforming the used operator).
</body>

View File

@ -0,0 +1,9 @@
<html>
<body>
Tries to simplify Object method calls such as toString, hashCode() and equals().
<!-- tooltip end -->
<br>Turns assertThat(object.toString()).isEqualTo(expression) into assertThat(object).hasToString(expression).
<br>Turns assertThat(object.hashCode()).isEqualTo(otherObject.hashCode()) into assertThat(object).hasSameHashCodeAs(otherObject).
<br>Turns assertThat(object.equals(otherObject)).isEqualTo(true) into assertThat(object).isEqualTo(otherObject) (and variations).
</body>
</html>

View File

@ -14,7 +14,6 @@ internal class AssertThatBinaryExpressionInspectionTest : AbstractCajonTest() {
myFixture.enableInspections(AssertThatBinaryExpressionInspection::class.java)
myFixture.configureByFile("BinaryExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Split binary expression out of assertThat()"), 149)
executeQuickFixes(myFixture, Regex.fromLiteral("Split equals() expression out of assertThat()"), 13)
myFixture.checkResultByFile("BinaryExpressionAfter.java")
}
}

View File

@ -0,0 +1,22 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatObjectExpressionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/ObjectExpression")
internal fun assertThat_with_certain_Object_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatObjectExpressionInspection::class.java)
myFixture.configureByFile("ObjectExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of actual expression and use assertThat().isEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of actual expression and use assertThat().isNotEqualTo() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace calls to hashCode() with hasSameHashCodeAs()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove toString() of actual expression and use assertThat().hasToString() instead"), 1)
myFixture.checkResultByFile("ObjectExpressionAfter.java")
}
}

View File

@ -13,14 +13,14 @@ internal class AssertThatStringExpressionInspectionTest : AbstractCajonTest() {
internal fun assertThat_with_certain_String_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatStringExpressionInspection::class.java)
myFixture.configureByFile("StringExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isEmpty() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of actual expression and use assertThat().isEqualTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equalsIgnoreCase() of actual expression and use assertThat().isEqualToIgnoringCase() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contentEquals() of actual expression and use assertThat().isEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of actual expression and use assertThat().contains() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove startsWith() of actual expression and use assertThat().startsWith() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove endsWith() of actual expression and use assertThat().endsWith() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isNotEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isNotEmpty() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of actual expression and use assertThat().isNotEqualTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equalsIgnoreCase() of actual expression and use assertThat().isNotEqualToIgnoringCase() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contentEquals() of actual expression and use assertThat().isNotEqualTo() instead"), 4)

View File

@ -146,12 +146,12 @@ public class BinaryExpression {
assertThat(numberObjAct).isGreaterThan(1);
assertThat(numberObjAct).isGreaterThan(1);
assertThat(numberObjAct).as("doh!").isEqualTo(numberObjExp);
assertThat(numberObjAct).isEqualTo(numberObjExp);
assertThat(numberObjAct).isEqualTo(numberObjExp);
assertThat(numberObjAct).isNotEqualTo(numberObjExp);
assertThat(numberObjAct).isNotEqualTo(numberObjExp);
assertThat(numberObjAct).isNotEqualTo(numberObjExp);
assertThat(numberObjAct.equals(numberObjExp)).as("doh!").isTrue();
assertThat(numberObjAct.equals(numberObjExp)).isEqualTo(true);
assertThat(numberObjAct.equals(numberObjExp)).isNotEqualTo(false);
assertThat(numberObjAct.equals(numberObjExp)).isFalse();
assertThat(numberObjAct.equals(numberObjExp)).isEqualTo(false);
assertThat(numberObjAct.equals(numberObjExp)).isNotEqualTo(true);
assertThat(stringAct).as("doh!").isSameAs(stringExp);
assertThat(stringAct).isSameAs(stringExp);
@ -160,12 +160,12 @@ public class BinaryExpression {
assertThat(stringAct).isNotSameAs(stringExp);
assertThat(stringAct).isNotSameAs(stringExp);
assertThat(stringAct).as("doh!").isEqualTo(stringExp);
assertThat(stringAct).isEqualTo(stringExp);
assertThat(stringAct).isEqualTo(stringExp);
assertThat(stringAct).isNotEqualTo(stringExp);
assertThat(stringAct).isNotEqualTo(stringExp);
assertThat(stringAct).isNotEqualTo(stringExp);
assertThat(stringAct.equals(stringExp)).as("doh!").isTrue();
assertThat(stringAct.equals(stringExp)).isEqualTo(true);
assertThat(stringAct.equals(stringExp)).isNotEqualTo(false);
assertThat(stringAct.equals(stringExp)).isFalse();
assertThat(stringAct.equals(stringExp)).isEqualTo(false);
assertThat(stringAct.equals(stringExp)).isNotEqualTo(true);
assertThat(stringAct).as("doh!").isNotSameAs(stringExp);
assertThat(stringAct).isNotSameAs(stringExp);
@ -194,6 +194,6 @@ public class BinaryExpression {
assertThat(primAct).as("doh!").isEqualTo(primExp).isEqualTo(primExp);
assertThat(primAct == primExp).isFalse().as("doh!").isEqualTo(true);
assertThat(numberObjAct).as("doh!").isEqualTo(numberObjExp).isEqualTo(numberObjExp);
assertThat(numberObjAct.equals(numberObjExp)).as("doh!").isTrue().isEqualTo(true);
}
}

View File

@ -0,0 +1,27 @@
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectExpression {
private void objectExpression() {
Integer object = 1;
Integer otherObject = 1;
boolean foo = false;
assertThat(object).hasToString("foo");
assertThat(object.toString()).isNotEqualTo("foo");
assertThat(object).hasSameHashCodeAs(otherObject);
assertThat(object.hashCode()).isEqualTo(123);
assertThat(object).as("doh!").isEqualTo(otherObject);
assertThat(object).isEqualTo(otherObject);
assertThat(object).isEqualTo(otherObject);
assertThat(object).isNotEqualTo(otherObject);
assertThat(object).isNotEqualTo(otherObject);
assertThat(object).isNotEqualTo(otherObject);
assertThat(object.equals(otherObject)).isEqualTo(foo);
assertThat(object).as("doh!").isEqualTo(otherObject).isEqualTo(otherObject);
}
}

View File

@ -0,0 +1,27 @@
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectExpression {
private void objectExpression() {
Integer object = 1;
Integer otherObject = 1;
boolean foo = false;
assertThat(object.toString()).isEqualTo("foo");
assertThat(object.toString()).isNotEqualTo("foo");
assertThat(object.hashCode()).isEqualTo(otherObject.hashCode());
assertThat(object.hashCode()).isEqualTo(123);
assertThat(object.equals(otherObject)).as("doh!").isTrue();
assertThat(object.equals(otherObject)).isEqualTo(true);
assertThat(object.equals(otherObject)).isNotEqualTo(false);
assertThat(object.equals(otherObject)).isFalse();
assertThat(object.equals(otherObject)).isEqualTo(false);
assertThat(object.equals(otherObject)).isNotEqualTo(true);
assertThat(object.equals(otherObject)).isEqualTo(foo);
assertThat(object.equals(otherObject)).as("doh!").isTrue().isEqualTo(true);
}
}

View File

@ -8,6 +8,7 @@ public class StringExpression {
assertThat(string).as("foo").isEmpty();
assertThat(string).isEmpty();
assertThat(string).isEmpty();
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualToIgnoringCase("foo");
@ -27,6 +28,7 @@ public class StringExpression {
assertThat(string).as("foo").isNotEmpty();
assertThat(string).isNotEmpty();
assertThat(string).isNotEmpty();
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualToIgnoringCase("foo");

View File

@ -7,6 +7,7 @@ public class StringExpression {
StringBuilder stringBuilder = new StringBuilder();
assertThat(string.isEmpty()).as("foo").isEqualTo(true);
assertThat(string.isEmpty()).isNotEqualTo(false);
assertThat(string.isEmpty()).isTrue();
assertThat(string.equals("foo")).isEqualTo(true);
assertThat(string.equals("foo")).isTrue();
@ -26,6 +27,7 @@ public class StringExpression {
assertThat(string.endsWith("foo")).isTrue();
assertThat(string.isEmpty()).as("foo").isEqualTo(false);
assertThat(string.isEmpty()).isNotEqualTo(true);
assertThat(string.isEmpty()).isFalse();
assertThat(string.equals("foo")).isEqualTo(false);
assertThat(string.equals("foo")).isFalse();