Last active
February 16, 2024 18:58
-
-
Save aevans-mms/d26e67ffde8c92d5ac458dbd7c3b3b51 to your computer and use it in GitHub Desktop.
Comparing different assertion methods and error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test: AgeVerificationTests.testCustomerAge_usingAssertTrue
test: AgeVerificationTests.testCustomerAge_usingAssertTrueWithMessage
test: AgeVerificationTests.testCustomerAge_usingAssertEquals
test: AgeVerificationTests.testCustomerAge_usingAssertEqualsWithMessage
test: AgeVerificationTests.testCustomerAge_usingAssertjIsEqualTo
test: AgeVerificationTests.testCustomerAge_usingAssertjIsTrue
test: AgeVerificationTests.testCustomerAge_usingAssertjIsTrueWithMessage
test: AgeVerificationTests.testCustomerAge_usingAssertjWithContext
test: AgeVerificationTests.testCustomerAge_withSoftAssertions