Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Introduction

In this tutorial, we'll explore the differences between a failure and an error in JUnit tests.

In short, failures are unfulfilled assertions while errors are due to abnormal test execution.

2. Sample Code

Let's consider a very simplistic example, namely a calculator class that has one method to divide two double values:

public static double divideNumbers(double dividend, double divisor) {  
    if (divisor == 0) { 
        throw new ArithmeticException("Division by zero!"); 
    return dividend / divisor; 

Note that Java doesn't actually throw an ArithmeticException on its own for double division – it returns Infinity or NaN.

3. Example Failure

When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.

In the following example, the assertion will fail, because the result of the division is 2 and not 15. Our assertion and the actual result simply don't match:

void whenDivideNumbers_thenExpectWrongResult() {
    double result = SimpleCalculator.divideNumbers(6, 3);
    assertEquals(15, result);

4. Example Error

Another possibility is that we have an unexpected situation during test execution, most likely due to an exception; for example, accessing a null reference will raise a RuntimeException.

Let's see an example, where the test will abort with an error because we're attempting to divide by zero which we explicitly guard against by throwing an exception in our calculator code:

void whenDivideByZero_thenThrowsException(){
    SimpleCalculator.divideNumbers(10, 0);

Now, we could fix this test by simply including the exception as one of our assertions.

void whenDivideByZero_thenAssertException(){
    assertThrows(ArithmeticException.class, () -> SimpleCalculator.divideNumbers(10, 0));

Then, if the exception is thrown, the test passes, but if not then that would be another failure.

5. Conclusion

Both failure and error in JUnit tests indicate an undesired situation, but their semantics are different. Failures notify of an invalid test result, errors indicate an unexpected test execution.

Also, please check out the example code at GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Comments are closed on this article!