JUnit Test Cases – Eclipse and Maven

Filed Under: JUnit

JUnit Test Cases are normal java class with methods to be executed by JUnit framework. JUnit 5 is a major upgrade from earlier versions.

Creating JUnit Test Cases in Eclipse

Latest Eclipse versions come with built-in support for JUnit test cases creation. We can easily create JUnit test cases class in Eclipse as shown below.

Go to New | JUnit Test Case in your Eclipse.

JUnit Test Cases Eclipse

In the next popup window, provide the test class name, its package, method stubs to generate etc. Note that Eclipse provides us option to create JUnit 3, JUnit 4 and JUnit Jupiter Test Cases. Unless your project is using the older version of JUnit and you haven’t migrated to the latest versions, it’s better to create the JUnit Jupiter test case.

JUnit Test Case Class

Once you click on “Finish” button, the test case class will be generated. You can run this class by going to Run | Run As | JUnit Test. You can also reach this menu by right-clicking in the Editor window or by selecting the class and then right click on it.

Eclipse Run JUnit Test Cases

JUnit Eclipse Run Configurations

You can also create run configurations to execute JUnit test cases. Run configurations are helpful in running multiple test classes at once by selecting the package, or selecting only a few methods to run in the test class.

eclipse junit test cases run configurations

When you run a JUnit test, a sample run configuration is automatically created. So you can just edit them as per your requirements.

JUnit Test Cases Execution with Maven Build

If you want your JUnit 5 test cases to be executed with maven build, you will have to configure maven-surefire-plugin with junit-platform-surefire-provider dependencies.


<build>
	<plugins>
		<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
	</plugins>
</build>

Here is a simple JUnit test class I have created in my example project.


package com.journaldev.annotations;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.journaldev.utils.MyUtils;

class MyUtilsTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
		System.out.println("Set Up Before Class - @BeforeAll");
	}

	@AfterAll
	static void tearDownAfterClass() throws Exception {
		System.out.println("Tear Down After Class - @AfterAll");
	}

	@BeforeEach
	void setUp() throws Exception {
		System.out.println("Set Up @BeforeEach");
	}

	@AfterEach
	void tearDown() throws Exception {
		System.out.println("Tear Down @AfterEach");
	}

	@Test
	void test_add() {
		assertEquals(10, MyUtils.add(5, 5));
	}

	@Test
	void test_reverse() {
		assertEquals("cba", MyUtils.reverse("abc"));
	}
}

When the mvn test command is executed, it produces the following snippet for our JUnit test class execution.


[INFO] Running com.journaldev.annotations.MyUtilsTest
Set Up Before Class - @BeforeAll
Set Up @BeforeEach
Tear Down @AfterEach
Set Up @BeforeEach
Tear Down @AfterEach
Tear Down After Class - @AfterAll
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.journaldev.annotations.MyUtilsTest

If you want to skip test cases execution, you can run the following command:


mvn clean install -Dmaven.test.skip=true

Summary

JUnit test cases help us in unit testing our code. Eclipse IDE provides great built-in support for JUnit test classes creation and execution. We also learned how to configure our maven project to execute JUnit Jupiter test cases from command line maven build.

You can check out JUnit example project with more examples at our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages