0
0
JUnittesting~5 mins

Test result publishing in JUnit

Choose your learning style9 modes available
Introduction

Test result publishing helps you see if your tests passed or failed. It shows the outcome clearly so you know if your software works well.

After running automated tests to check if code changes broke anything.
When you want to share test results with your team for review.
To keep a record of test outcomes for future reference.
When integrating tests into a build system that needs test reports.
To quickly find which tests failed and fix problems.
Syntax
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ExampleTest {
    @Test
    void testSomething() {
        assertEquals(4, 2 + 2);
    }
}

JUnit automatically publishes test results when you run tests in an IDE or build tool.

Test results include pass/fail status and error messages if any.

Examples
This test will pass and JUnit will publish a success result.
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class SimpleTest {
    @Test
    void testAddition() {
        assertEquals(4, 2 + 2);
    }
}
This test will fail and JUnit will publish a failure result with details.
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class FailingTest {
    @Test
    void testFailure() {
        assertEquals(5, 2 + 2);
    }
}
Sample Program

This class has three tests. Two will pass, one will fail. When run, JUnit publishes results showing which tests passed and which failed.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    void testAddition() {
        int sum = 3 + 7;
        assertEquals(10, sum);
    }

    @Test
    void testSubtraction() {
        int difference = 10 - 5;
        assertEquals(5, difference);
    }

    @Test
    void testFailureExample() {
        int product = 2 * 3;
        assertEquals(5, product); // This will fail
    }
}
OutputSuccess
Important Notes

JUnit test results are usually shown in your IDE's test runner or build tool console.

Test result publishing is automatic; you just need to run your tests.

Failing tests include error messages to help you find the problem quickly.

Summary

Test result publishing shows if tests pass or fail.

JUnit automatically publishes results when tests run.

Use test results to fix bugs and improve your code.