Skip to content

Instantly share code, notes, and snippets.

@aevans-mms
Last active February 16, 2024 18:58
Show Gist options
  • Save aevans-mms/d26e67ffde8c92d5ac458dbd7c3b3b51 to your computer and use it in GitHub Desktop.
Save aevans-mms/d26e67ffde8c92d5ac458dbd7c3b3b51 to your computer and use it in GitHub Desktop.
Comparing different assertion methods and error messages
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class Customer
{
String name;
Integer age;
}
public class AgeVerificationTests
{
Customer customer;
int expectedAge = 39;
@BeforeEach
public void setup()
{
customer = new Customer();
customer.name = "John Doe";
customer.age = 18;
}
/** Don't do this, assertTrue() loses data that helps us debug **/
@Test
public void testCustomerAge_usingAssertTrue()
{
assertTrue(customer.age >= 18);
assertTrue(customer.age == expectedAge);
}
/** A custom message can show the expected results **/
@Test void testCustomerAge_usingAssertTrueWithMessage()
{
assertTrue(customer.age >= expectedAge, String.format("age %d was not same as expected %d", customer.age, expectedAge));
}
/** This is better because assertEquals() will show the comparison on failure **/
@Test
public void testCustomerAge_usingAssertEquals()
{
assertEquals(customer.age, expectedAge);
}
/** This is even better because message provides context **/
@Test
public void testCustomerAge_usingAssertEqualsWithMessage()
{
assertEquals(customer.age, expectedAge, "checking customer age");
}
/** using AssertJ **/
@Test
public void testCustomerAge_usingAssertjIsEqualTo()
{
assertThat(customer.age).isEqualTo(expectedAge);
}
/** This has the same problem as assertTrue() - We don't know what is being tested **/
@Test
public void testCustomerAge_usingAssertjIsTrue()
{
assertThat(customer.age == expectedAge).isTrue();
}
/** This gives us some context, but we have to enter it ourselves **/
@Test
public void testCustomerAge_usingAssertjIsTrueWithMessage()
{
assertThat(customer.age).overridingErrorMessage(String.format("age %d was not same as expected %d", customer.age, expectedAge)).isEqualTo(expectedAge);
}
/** This adds context and gives us the comparison **/
@Test
public void testCustomerAge_usingAssertjWithContext()
{
assertThat(customer.age).as("checking customer age").isEqualTo(expectedAge);
}
/** Soft Assertions allow you to perform multiple checks **/
@Test
public void testCustomerAge_withSoftAssertions()
{
var softly = new SoftAssertions();
softly.assertThat(customer.age).as("check that customer age has been set").isNotNull();
softly.assertThat(customer.age).as("check that customer age is correct").isEqualTo(expectedAge);
softly.assertThat(customer.age).as("make sure customer is not a minor").isGreaterThanOrEqualTo(18);
softly.assertAll();
}
}
@aevans-mms
Copy link
Author

Run this set of tests to see what each assertion and error message looks like to compare the results.

@aevans-mms
Copy link
Author

aevans-mms commented Feb 16, 2024

test: AgeVerificationTests.testCustomerAge_usingAssertTrue

org.opentest4j.AssertionFailedError: 
Expected :true
Actual   :false
at AgeVerificationTests.testCustomerAge_usingAssertTrue(AgeVerificationTests.java:38)

test: AgeVerificationTests.testCustomerAge_usingAssertTrueWithMessage

org.opentest4j.AssertionFailedError: age 18 was not same as expected 39 ==> 
Expected :true
Actual   :false
at AgeVerificationTests.testCustomerAge_usingAssertTrueWithMessage(AgeVerificationTests.java:45)

test: AgeVerificationTests.testCustomerAge_usingAssertEquals

org.opentest4j.AssertionFailedError: 
Expected :18
Actual   :39
at AgeVerificationTests.testCustomerAge_usingAssertEquals(AgeVerificationTests.java:53)

test: AgeVerificationTests.testCustomerAge_usingAssertEqualsWithMessage

org.opentest4j.AssertionFailedError: checking customer age ==> 
Expected :18
Actual   :39
at AgeVerificationTests.testCustomerAge_usingAssertEqualsWithMessage(AgeVerificationTests.java:61)

test: AgeVerificationTests.testCustomerAge_usingAssertjIsEqualTo

org.opentest4j.AssertionFailedError: 
expected: 39
 but was: 18
Expected :39
Actual   :18
at AgeVerificationTests.testCustomerAge_usingAssertjIsEqualTo(AgeVerificationTests.java:69)

test: AgeVerificationTests.testCustomerAge_usingAssertjIsTrue

org.opentest4j.AssertionFailedError: 
Expecting value to be true but was false
Expected :true
Actual   :false
at AgeVerificationTests.testCustomerAge_usingAssertjIsTrue(AgeVerificationTests.java:77)

test: AgeVerificationTests.testCustomerAge_usingAssertjIsTrueWithMessage

java.lang.AssertionError: age 18 was not same as expected 39
at AgeVerificationTests.testCustomerAge_usingAssertjIsTrueWithMessage(AgeVerificationTests.java:85)

test: AgeVerificationTests.testCustomerAge_usingAssertjWithContext

org.opentest4j.AssertionFailedError: [checking customer age] 
expected: 39
 but was: 18
Expected :39
Actual   :18
at AgeVerificationTests.testCustomerAge_usingAssertjWithContext(AgeVerificationTests.java:93)

test: AgeVerificationTests.testCustomerAge_withSoftAssertions

[check that customer age is correct] 
expected: 39
 but was: 18
at AgeVerificationTests.testCustomerAge_withSoftAssertions(AgeVerificationTests.java:103)

Comparison Failure: 
Expected :39
Actual   :18

org.assertj.core.error.AssertJMultipleFailuresError: 
Multiple Failures (1 failure)
-- failure 1 --
[check that customer age is correct] 
expected: 39
 but was: 18
at AgeVerificationTests.testCustomerAge_withSoftAssertions(AgeVerificationTests.java:103)
at AgeVerificationTests.testCustomerAge_withSoftAssertions(AgeVerificationTests.java:105)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment