0
0
JUnittesting~15 mins

Gradle test task in JUnit - Build an Automation Script

Choose your learning style9 modes available
Run JUnit tests using Gradle test task
Preconditions (2)
Step 1: Open terminal or command line at the project root directory
Step 2: Run the command 'gradle test'
Step 3: Observe the console output for test execution results
Step 4: Verify that all tests pass without errors or failures
✅ Expected Result: Gradle runs all JUnit tests and reports test summary showing all tests passed
Automation Requirements - JUnit 5 with Gradle
Assertions Needed:
Verify that the Gradle test task completes successfully
Verify that the test report contains all executed tests
Verify that no tests failed or were skipped
Best Practices:
Use Gradle's built-in test task to run tests
Keep test classes and methods annotated properly with @Test
Use Gradle's test logging to capture detailed test results
Do not hardcode test paths; rely on Gradle conventions
Automated Solution
JUnit
/*
 * This is a sample JUnit 5 test class to be run by Gradle test task.
 * Save this file under src/test/java/com/example/ExampleTest.java
 */
package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ExampleTest {

    @Test
    void addition_shouldReturnCorrectSum() {
        int result = 2 + 3;
        assertEquals(5, result, "2 + 3 should equal 5");
    }
}

/*
 * To run tests, open terminal at project root and run:
 * gradle test
 *
 * Gradle will compile and run all tests under src/test/java.
 * Test results will be shown in console and saved under build/reports/tests/test/index.html
 */

This code defines a simple JUnit 5 test class with one test method.

The test method checks that 2 + 3 equals 5 using assertEquals. This is a basic assertion to verify test execution.

Gradle automatically detects and runs all tests in src/test/java when you run gradle test.

The test results appear in the console and also generate an HTML report under build/reports/tests/test/index.html.

This setup follows Gradle conventions and JUnit 5 best practices for test automation.

Common Mistakes - 5 Pitfalls
{'mistake': 'Not adding JUnit 5 dependency in build.gradle', 'why_bad': "Without the JUnit 5 dependency, tests won't compile or run, causing build failures.", 'correct_approach': "Add the JUnit 5 dependency in the dependencies block of build.gradle, e.g., implementation 'org.junit.jupiter:junit-jupiter:5.9.3' and configure test task to use JUnit Platform."}
{'mistake': 'Placing test classes outside src/test/java directory', 'why_bad': "Gradle only runs tests located in src/test/java by default, so tests outside won't be executed.", 'correct_approach': 'Always place test classes under src/test/java following package structure.'}
Using JUnit 4 annotations with JUnit 5 dependency
{'mistake': "Running tests with 'gradle build' and ignoring test failures", 'why_bad': "'gradle build' runs tests but may continue build even if tests fail, hiding test failures.", 'correct_approach': "Use 'gradle test' to focus on tests and check test reports carefully."}
{'mistake': 'Hardcoding test class names in Gradle test task configuration', 'why_bad': 'Hardcoding limits flexibility and requires changes when tests change.', 'correct_approach': "Use Gradle's default test discovery which runs all tests automatically."}
Bonus Challenge

Now add data-driven testing with 3 different input values for addition

Show Hint