JUNIT / CATATAN</>↗JUnit08 Jul 2026
JUnit itself does not require one specific naming convention for test methods, especially in JUnit 5. A test method is recognized because it is annotated with @Test, not because of its name. That said, good test names are crucial because they explain what behavior is being tested. 1. Test Class Naming A common convention is […]
JUNIT / CATATAN</>↗JUnit08 Jul 2026
Testing Exceptions with assertThrows() in JUnit 5 Use assertThrows() when you expect a piece of code to throw a specific exception. Basic Syntax ExceptionType exception = assertThrows( ExceptionType.class, () -> { // code that should throw the exception } ); For JUnit 5, import it like this: import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrows; Simple Example import […]
JUNIT / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 […]