CI pipeline test stage in JUnit - Build an Automation Script
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class SampleUnitTest { @Test void testAddition() { int result = 2 + 3; assertEquals(5, result, "2 + 3 should equal 5"); } @Test void testStringContains() { String text = "Hello CI Pipeline"; assertTrue(text.contains("CI"), "Text should contain 'CI'"); } @Test void testArrayNotEmpty() { int[] numbers = {1, 2, 3}; assertNotEquals(0, numbers.length, "Array should not be empty"); } }
This JUnit 5 test class SampleUnitTest contains three simple unit tests.
Each test method is annotated with @Test so the CI pipeline test stage will detect and run them automatically.
Assertions check expected outcomes: assertEquals verifies addition, assertTrue checks string content, and assertNotEquals ensures the array is not empty.
When the CI pipeline runs mvn test, it will execute these tests and report results. If all assertions pass, the test stage will succeed.
This setup ensures the CI pipeline test stage runs unit tests and reports results as expected.
Now add data-driven testing with 3 different inputs for a method that checks if a number is even
Practice
Solution
Step 1: Understand the role of the test stage
The test stage runs automated tests to check if the code works as expected.Step 2: Identify the correct purpose
Running tests helps catch errors early before code is shared or deployed.Final Answer:
To run automated tests and verify code correctness -> Option DQuick Check:
Test stage = run tests [OK]
- Confusing test stage with deployment
- Thinking test stage merges code
- Assuming test stage creates docs
Solution
Step 1: Identify the build tool for JUnit tests
JUnit tests are commonly run using Maven with the commandmvn test.Step 2: Check other options
npm run testis for Node.js projects,docker buildbuilds images, andgit commitcommits code.Final Answer:
mvn test -> Option AQuick Check:
JUnit test command = mvn test [OK]
- Using npm commands for Java projects
- Confusing build commands with test commands
- Running git commands instead of tests
steps:
- name: Run tests
run: mvn test
continue-on-error: falseWhat happens if a JUnit test fails during this step?
Solution
Step 1: Understand 'continue-on-error: false'
This setting means the pipeline will stop if the command fails.Step 2: Recognize test failure effect
If a JUnit test fails,mvn testreturns an error, so the pipeline stops and marks failure.Final Answer:
The pipeline stops and marks the test stage as failed -> Option AQuick Check:
continue-on-error false + test fail = stop pipeline [OK]
- Assuming pipeline continues despite failure
- Thinking tests auto-retry on failure
- Believing test stage is skipped on failure
Solution
Step 1: Check test report generation
JUnit reports must be generated and saved as artifacts for the UI to display them.Step 2: Understand impact of missing reports
If reports are missing or not saved, the UI cannot show test results even if tests ran.Final Answer:
JUnit report files are not generated or not saved as artifacts -> Option CQuick Check:
Missing reports = no UI display [OK]
- Assuming syntax errors cause missing reports
- Thinking pipeline skipping always hides reports
- Believing missing config file causes this specific issue
Solution
Step 1: Ensure tests run and fail build on failure
Usingmvn testwithcontinue-on-error: falsestops the build on test failure.Step 2: Save test reports as artifacts
Saving JUnit reports as artifacts allows review of test results even if build fails.Final Answer:
Run tests with mvn test, save reports as artifacts, and set continue-on-error: false -> Option BQuick Check:
Fail build + save reports = mvn test + artifacts + continue-on-error false [OK]
- Setting continue-on-error true hides failures
- Not saving reports loses test details
- Skipping tests but expecting reports
Ready to prove it?
Pass the test and this pattern moves unknown
