How to run JUnit tests with Gradle?

Learn how to run junit tests with gradle? with practical examples, diagrams, and best practices. Covers java, gradle, junit4 development techniques with visual explanations.

How to Run JUnit Tests with Gradle

Hero image for How to run JUnit tests with Gradle?

Learn to integrate and execute JUnit 4 tests in your Java projects using Gradle, covering setup, execution, and reporting.

Running automated tests is a crucial part of modern software development. JUnit is a widely adopted testing framework for Java, and Gradle is a powerful build automation tool. This article will guide you through setting up and executing JUnit 4 tests within a Gradle project, ensuring your code is robust and reliable.

Setting Up Your Gradle Project for JUnit

To begin, you need to configure your Gradle project to recognize and run JUnit tests. This involves adding the necessary dependencies and applying the java plugin, which implicitly includes the junit-platform plugin for test execution. We'll focus on JUnit 4, but the principles are similar for JUnit 5.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.10.0'
}

test {
    useJUnitPlatform()
    testLogging {
        events "PASSED", "SKIPPED", "FAILED"
    }
}

Writing Your First JUnit Test

Once your build.gradle is configured, you can start writing your JUnit tests. By convention, test classes are placed in src/test/java (for Java projects) or src/test/kotlin (for Kotlin projects). Let's create a simple test for a basic calculator class.

// src/main/java/com/example/Calculator.java
package com.example;

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

    public int subtract(int a, int b) {
        return a - b;
    }
}
// src/test/java/com/example/CalculatorTest.java
package com.example;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalculatorTest {

    @Test
    public void testAdd() {
        Calculator calculator = new Calculator();
        assertEquals("Addition should work correctly", 5, calculator.add(2, 3));
    }

    @Test
    public void testSubtract() {
        Calculator calculator = new Calculator();
        assertEquals("Subtraction should work correctly", 1, calculator.subtract(3, 2));
    }
}

Executing Tests with Gradle

Gradle provides a straightforward way to execute your tests from the command line. The test task is automatically created when you apply the java plugin. You can run all tests, specific tests, or generate reports.

flowchart TD
    A[Start] --> B{Run `gradle test`}
    B --> C[Gradle compiles source and test code]
    C --> D[Gradle executes JUnit tests]
    D -- All tests pass --> E[Build SUCCESSFUL]
    D -- Some tests fail --> F[Build FAILED]
    E --> G[Generate Test Report]
    F --> G

Flowchart of Gradle Test Execution Process

1. Run All Tests

To execute all tests in your project, navigate to your project's root directory in the terminal and run the gradle test command.

2. Run Specific Test Class

To run a single test class, use the --tests option followed by the fully qualified class name, e.g., gradle test --tests com.example.CalculatorTest.

3. Run Specific Test Method

To run a specific test method within a class, specify the method name after the class name, e.g., gradle test --tests com.example.CalculatorTest.testAdd.

4. Clean Build and Rerun Tests

Sometimes you might want to clean your build directory before running tests to ensure a fresh build. Use gradle clean test.