0
0
JunitHow-ToBeginner ยท 3 min read

How to Run JUnit Test: Simple Steps to Execute Your Tests

To run a JUnit test, you can use an IDE like IntelliJ or Eclipse by right-clicking the test class or method and selecting 'Run'. Alternatively, run tests from the command line using mvn test for Maven or gradle test for Gradle projects, or use the JUnit Platform Console Launcher for standalone execution.
๐Ÿ“

Syntax

JUnit tests are methods annotated with @Test inside a class. To run tests, you execute the test class or method using a test runner provided by your IDE or build tool.

Key parts:

  • @Test: Marks a method as a test.
  • Test class: Contains one or more test methods.
  • Test runner: Executes the tests and reports results.
java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {
    @Test
    void addition() {
        assertEquals(5, 2 + 3);
    }
}
๐Ÿ’ป

Example

This example shows a simple JUnit 5 test class with one test method. Running this test will check if 2 + 3 equals 5.

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

public class CalculatorTest {
    @Test
    void addition() {
        assertEquals(5, 2 + 3);
    }
}
Output
Test run finished after 50 ms [ 1 containers found ] [ 0 containers skipped ] [ 1 containers started ] [ 0 containers aborted ] [ 1 containers successful ] [ 0 containers failed ] [ 1 tests found ] [ 0 tests skipped ] [ 1 tests started ] [ 0 tests aborted ] [ 1 tests successful ] [ 0 tests failed ]
โš ๏ธ

Common Pitfalls

Common mistakes when running JUnit tests include:

  • Not annotating methods with @Test, so tests are not detected.
  • Using JUnit 4 annotations with JUnit 5 dependencies or vice versa.
  • Running tests without a proper test runner or build tool configuration.
  • Forgetting to include JUnit in the project dependencies.

Always check your project setup and annotations.

java
/* Wrong: Missing @Test annotation, test won't run */
public class WrongTest {
    void testAddition() {
        assertEquals(5, 2 + 3);
    }
}

/* Right: Proper @Test annotation */
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RightTest {
    @Test
    void testAddition() {
        assertEquals(5, 2 + 3);
    }
}
๐Ÿ“Š

Quick Reference

Here is a quick summary of how to run JUnit tests:

MethodHow to RunNotes
IDE (IntelliJ, Eclipse)Right-click test class/method โ†’ RunFast, visual results, easy debugging
MavenRun mvn test in project rootRuns all tests in the project
GradleRun gradle test in project rootRuns all tests in the project
JUnit Console LauncherUse java -jar junit-platform-console-standalone.jar --scan-classpathStandalone test execution without IDE or build tool
โœ…

Key Takeaways

Use @Test annotation to mark test methods for JUnit to run them.
Run tests easily from your IDE by right-clicking and selecting 'Run'.
Use build tools like Maven or Gradle commands to run all tests from the command line.
Ensure your project includes the correct JUnit dependencies and versions.
Check for missing annotations or misconfigured runners if tests do not execute.