Skip to main content

On This Page

How to Print JUnit Assertion Results

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

How to Print JUnit Assertion Results

JUnit tests typically indicate pass or fail status, but detailed assertion logging is crucial for debugging and monitoring. This article explores methods for printing assertion results, enhancing visibility into test execution, and aiding in faster issue resolution.

Why This Matters

Ideal testing models assume immediate failure identification, but real-world scenarios often involve complex test cases and distributed systems. Without detailed assertion logging, pinpointing the exact cause of a failure can be time-consuming and expensive – potentially costing engineering hours and delaying releases.

Key Insights

  • AssertionFailedError: JUnit’s core exception for failed assertions.
  • TestWatchers & Callbacks: JUnit 5 provides lifecycle hooks for custom logging and behavior.
  • Logging Levels: Utilizing INFO for passing assertions and ERROR for failures provides clear distinction in logs.

Working Example

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.opentest4j.AssertionFailedError;
import org.junit.jupiter.api.BeforeEachCallback;
import org.junit.jupiter.api.TestWatcher;
import org.junit.jupiter.api.ExtensionContext;

public class TestService {
    public boolean successfulCall() {
        return true;
    }
    public boolean failedCall() {
        return false;
    }
    public boolean exceptionCall() {
        throw new RuntimeException("Service error");
    }
}

class TestServiceTest {
    private static final Logger logger = LoggerFactory.getLogger(TestServiceTest.class);
    TestService service = new TestService();

    @Test
    void whenSuccessfulCall_thenSuccessMessagePrintedIntoLog() {
        logger.info("Testing successful call...");
        assertTrue(service.successfulCall(), "Service should return true for successful call");
        logger.info("✓ Successful call assertion passed");
    }
}

public class LoggingAssertions {
    private static final Logger logger = LoggerFactory.getLogger(LoggingAssertions.class);
    public static void assertAll(AssertionWithMessage... assertions) {
        boolean failed = false;
        for (AssertionWithMessage assertion : assertions) {
            try {
                assertion.doAssert();
                logger.info("✓ {}", assertion.getMessage());
            } catch (AssertionError e) {
                failed = true;
                logger.error("✗ {} - {}", assertion.getMessage(), e.getMessage());
            }
        }
        if (failed) {
            throw new AssertionError("One of the assertions was failed. See logs for details");
        }
    }
}

@ExtendWith(TestResultLogger.class)
class TestServiceWithTestWatcherTest {
    TestService service = new TestService();
    @Test
    void whenSuccessfulCall_thenTrueShouldBeReturned() {
        assertTrue(service.successfulCall());
    }
}

public class TestResultLogger implements TestWatcher, BeforeEachCallback {
    private static final Logger logger = LoggerFactory.getLogger(TestResultLogger.class);
    @Override
    public void beforeEach(ExtensionContext context) throws Exception {
        logger.info("Testing {}", context.getDisplayName());
    }
}

Practical Applications

  • Microservice Testing: Logging assertion results in a distributed microservice architecture helps quickly identify failing components.
  • Pitfall: Overly verbose logging can obscure important information; focus on logging assertion-specific details rather than general test execution flow.

References:

Continue reading

Next article

Magecart Campaign Steals Credit Card Data From E-commerce Sites Since 2022

Related Content