Practical Guide for JUnit 5

By Bhushan Nikhar | Simple Technology

JUnit Testing Illustration

JUnit 5 is one of the most popular frameworks for unit testing in Java. It offers a modern and flexible testing architecture that simplifies writing and maintaining test cases.

This guide is written for developers who already know Java and want to quickly learn JUnit 5 to start writing effective and clean test cases.

Default Test Behaviour

By default, JUnit 5 considers a test passed unless it explicitly fails. This is opposite to JUnit 4, where tests were considered failed unless explicitly passed.

JUnit 5 Example

@Test
void junit5_default_test_pass() {  
    //Given
    //When
    //Then
}

✅ junit5_default_test_pass — Passed

JUnit 4 Example

@Test
public void junit4_default_test_fail() {
    //Given
    //When
    //Then
}

❌ junit4_default_test_fail — Failed

Note: In JUnit 5, test methods don’t need the public modifier — omitting it is recommended.

Best Practices for JUnit 5

  • Annotate test methods with @Test.
  • Use the Given–When–Then format.
  • Name tests descriptively (e.g. should_return_true_when_evenNumberPassed()).
  • Avoid access modifiers unless necessary.
  • Cover edge cases using separate test methods.
@Test  
void should_return_true_when_evenNumberPassed() {  
    //Given

    //When

    //Then
}

Assertions in JUnit 5

Assertions verify whether expected results match actual outcomes. Common ones include assertTrue(), assertFalse(), assertEquals(), assertNull(), assertArrayEquals(), and assertThrows().

Checking Boolean Values

Method Under Test

public static boolean isEven(int num) {
    return (num % 2 == 0);
}
@Test  
void should_return_true_when_evenNumberPassed() {
    //Given
    int num = 4;

    //When
    boolean result = Calculator.isEven(num); 

    //Then
    assertTrue(result);  
}

✅ should_return_true_when_evenNumberPassed — Passed

@Test  
void should_return_false_when_oddNumberPassed() {
    //Given
    int num = 5;

    //When
    boolean result = Calculator.isEven(num); 

    //Then
    assertFalse(result);  
}

✅ should_return_false_when_oddNumberPassed — Passed

Checking for Null

Method Under Test

public static Integer[] sortArray(Integer[] inputArray) {  
    if (inputArray == null || inputArray.length == 0) {  
        return null;  
    }  
    Integer[] sortedArray = Arrays.copyOf(inputArray, inputArray.length);  
    Arrays.sort(sortedArray);  
    return sortedArray;  
}
@Test  
void should_return_null_when_passed_emptyArray() {  
    //Given  
    Integer[] array = {};  

    //When  
    Integer[] actualSortedArray = Calculator.sortArray(array);  

    //Then  
    assertNull(actualSortedArray);  
}

✅ should_return_null_when_passed_emptyArray — Passed

@Test  
void should_return_sortedArray_when_passed_inputArray() {  
    //Given  
    Integer[] array = {3, 2, 1, 4};  

    //When  
    Integer[] actualSortedArray = Calculator.sortArray(array);  

    //Then  
    assertNotNull(actualSortedArray);  
}

✅ should_return_sortedArray_when_passed_inputArray — Passed

Checking for Equal Values

Method Under Test

public static int add(int x, int y) {  
    return x + y;  
}
@Test
void should_return_Sum_when_passed_twoIntegers() {  
    //Given  
    int x = 2;  
    int y = 3;  
    int expectedSum = 5;  

    //When  
    int actualSum = Calculator.add(x, y);  

    //Then  
    assertEquals(expectedSum, actualSum);  
}

✅ should_return_Sum_when_passed_twoIntegers — Passed

@Test  
void should_not_return_WrongSum_when_passed_twoIntegers() {  
    //Given  
    int x = 2;  
    int y = 3;  
    int expectedSum = 6;  

    //When  
    int actualSum = Calculator.add(x, y);  

    //Then  
    assertNotEquals(expectedSum, actualSum);  
}

✅ should_not_return_WrongSum_when_passed_twoIntegers — Passed

Checking for Equal Arrays

Method Under Test

public static Integer[] sortArray(Integer[] inputArray) {  
    if (inputArray == null || inputArray.length == 0) {  
        return null;  
    }  
    Integer[] sortedArray = Arrays.copyOf(inputArray, inputArray.length);  
    Arrays.sort(sortedArray);  
    return sortedArray;  
}
@Test  
void should_return_sortedArray_when_passed_inputArray() {  
    //Given  
    Integer[] array = {3, 2, 1, 4};  
    Integer[] expectedArray = {1, 2, 3, 4};  

    //When  
    Integer[] actualSortedArray = Calculator.sortArray(array);  

    //Then  
    assertArrayEquals(expectedArray, actualSortedArray);  
}

✅ should_return_sortedArray_when_passed_inputArray — Passed

@Test  
void should_not_return_wrongSortedArray_when_passed_inputArray() {  
    //Given  
    Integer[] array = {3, 2, 1, 4};  
    Integer[] expectedArray = {1, 3, 2, 4};  

    //When  
    Integer[] actualSortedArray = Calculator.sortArray(array);  

    //Then  
    assertNotEquals(expectedArray, actualSortedArray);  
}

✅ should_not_return_wrongSortedArray_when_passed_inputArray — Passed

Checking for Exceptions

Method Under Test

public static int division(int x, int y) {  
    if (y == 0)  
        throw new ArithmeticException("Division by zero is not allowed.");  
    return x / y;  
}
@Test  
void should_return_ArithmeticException_when_passed_zeroForSecondArgument() {  
    //Given  
    int x = 4;  
    int y = 0;  

    //When  
    Executable executable = () -> Calculator.division(x, y);  

    //Then  
    assertThrows(ArithmeticException.class, executable);  
}

✅ should_return_ArithmeticException_when_passed_zeroForSecondArgument — Passed

@Test 
void testDivideDoesNotThrow() { 
    //Given
    Calculator calculator = new Calculator(); 

    //When & Then
    assertDoesNotThrow(() -> calculator.divide(10, 2), "Method should not throw an exception"); 
}

✅ testDivideDoesNotThrow — Passed

Conclusion

JUnit 5 provides a modern way to write unit tests in Java. Following best practices and mastering assertions help ensure your code is reliable, maintainable, and bug-free.

Start small — write tests for existing methods, and soon, writing unit tests will become second nature.

Author: Bhushan Nikhar | Blog: Simple Technology | Category: Java Testing, JUnit 5, Best Practices