0
0
JUnittesting~15 mins

Why CI integration enables continuous quality in JUnit - Automation Benefits in Action

Choose your learning style9 modes available
Verify that CI integration runs tests automatically on code commit
Preconditions (3)
Step 1: Commit and push a code change to the repository
Step 2: Observe the CI pipeline triggered automatically
Step 3: Wait for the CI pipeline to run the JUnit tests
Step 4: Check the test results in the CI pipeline report
✅ Expected Result: The CI pipeline runs automatically on commit, executes all JUnit tests, and reports test results showing tests passed or failed
Automation Requirements - JUnit 5
Assertions Needed:
Verify that the test method executes and passes
Verify that the test output matches expected results
Best Practices:
Use @Test annotation for test methods
Use assertions from org.junit.jupiter.api.Assertions
Keep tests independent and repeatable
Use descriptive test method names
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @Test
    void additionShouldReturnCorrectSum() {
        Calculator calculator = new Calculator();
        int result = calculator.add(2, 3);
        assertEquals(5, result, "2 + 3 should equal 5");
    }
}

class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

This JUnit 5 test class CalculatorTest contains one test method additionShouldReturnCorrectSum. It creates a Calculator object and calls its add method with inputs 2 and 3.

The test uses assertEquals to check that the result is 5. This assertion ensures the method works correctly.

When this test is committed and pushed to a Git repository with CI configured, the CI tool will automatically run this test. The test passing confirms the code quality is maintained continuously.

We use @Test annotation to mark the test method, and clear assertion messages to explain failures if any.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not using @Test annotation on test methods', 'why_bad': "JUnit will not recognize the method as a test, so it won't run automatically in CI", 'correct_approach': 'Always annotate test methods with @Test to ensure they are executed'}
Hardcoding expected results incorrectly
Writing tests that depend on external state or order
Bonus Challenge

Now add data-driven testing with 3 different input pairs for the add method

Show Hint