JUNIT / NOTES</>↗JUnit08 Jul 2026
In JUnit 5, you can group multiple assertions using assertAll(). This lets JUnit run all assertions in the group, even if one fails, and then report all failures together. Basic Syntax import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class UserTest { @Test void testUserDetails() { User user = new User("Alice", 25, "alice@example.com"); assertAll("User details", () -> assertEquals("Alice", […]
JUNIT / NOTES</>↗JUnit08 Jul 2026
To test null and non-null values in JUnit, use these assertions: assertNull(value) — passes if the value is null assertNotNull(value) — passes if the value is not null In JUnit 5, these methods are available from org.junit.jupiter.api.Assertions. Basic Example import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNotNull; class NullCheckTest { @Test void valueShouldBeNull() { String [...
JUNIT / NOTES</>↗JUnit08 Jul 2026
Use assertTrue() when you expect a boolean condition to be true, and assertFalse() when you expect it to be false. They are JUnit assertions, most commonly used in JUnit 5 like this: import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; class BooleanConditionTest { @Test void shouldCheckBooleanConditions() { String message = "Hello JUnit"; assertTrue(message.contains("JUnit"));...
JUNIT / NOTES</>↗JUnit08 Jul 2026
In JUnit, you use assertEquals() to test whether an actual value matches an expected value. The basic syntax is: assertEquals(expectedValue, actualValue); The first argument is what you expect. The second argument is what your code actually produced. Basic Example import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class CalculatorTest { @Test void shouldAddTwoNumbers() { int result = 2 […]
JUNIT / NOTES</>↗JUnit07 Jul 2026
In JUnit, assertions are used to verify that your code produces the expected result. If an assertion fails, the test fails. JUnit 5 assertions are provided by the org.junit.jupiter.api.Assertions class. Basic Example import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class CalculatorTest { @Test void testAddition() { int result = 2 + 3; assertEquals(5, result); } } In […]
JUNIT / NOTES</>↗JUnit07 Jul 2026
Using @Test in JUnit In JUnit, @Test is an annotation that marks a method as a test method. When you run your tests, JUnit looks for methods annotated with @Test and executes them automatically. 1. Add the Correct Import For JUnit 5, use: import org.junit.jupiter.api.Test; You will usually also import assertions such as: import static […]