Home Maven Maven - How to run Single Test

Maven – How to run Single Test

In this quick tutorial, we discuss how to run single test or specific unit tests using Maven with ample of examples.

1. Running Tests with Maven

By default maven runs all the tests in test phase. Often it’s require to run single test or specific tests. Following image illustrates an over view of how to run unit tests with Maven.

how to run single test with maven

Before looking into examples, let’s have a look into project structure and configuration that used for testing.

Project Structure:

Maven Configuration:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javabydeveloper</groupId>
	<artifactId>run-maven-tests</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>run-maven-tests</name>
	<url>https://javabydeveloper.com/junit-5-tutorial/</url>

	<properties>
		<!-- Dependency versions -->
		<junit.jupiter.version>5.5.2</junit.jupiter.version>
		<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>

		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>

	</properties>

	<!-- Jupiter API for writing tests -->
	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<!-- Maven Surefire plugin to run tests -->
	<build>
		<plugins>
			<!-- plugin to run test cases from maven -->
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>${maven-surefire-plugin.version}</version>
			</plugin>
		</plugins>
	</build>
</project>

Quick look at Test cases:

public class MathUtilTest {
	
	@Test
	void test_add() {
		assertEquals(5, MathUtil.add(3, 2),"test_Add()");
	}
	
	@Test
	void test_multiply() {
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_devide() {
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void test_is_prime() {
		assertTrue(MathUtil.isPrime(13));
	}
}
public class MessageUtilTest {

	@Test
	void msg_add_test() {
		assertEquals("Hello Dear!", MessageUtil.msg_add("Hello", "Dear!"));
	}

	@Test 
	void msg_length_test() {
		assertEquals(4, MessageUtil.msg_length("Five"));
	}

	@Test
	void is_length_even() {
		assertEquals(true, MessageUtil.isLengthEven("Four"));
	}
}
public class Payment_Test {

	@Test
	void payment_success_test() {
		assertEquals("00", Payment.doPay("SUCCESS"));
	}

	@Test 
	void payment_decline_test() {
		assertEquals("100", Payment.doPay("DECLINE"));
	}

	@Test
	void invalid_expiry_date_test() {
		assertEquals("102", Payment.doPay("EXPIRED"));
	}
}

Now let’s see how to run single test or specific tests from above test cases with Maven.

1.1. Run all tests

To run all the unit tests with Maven run command $ mvn test   or $ mvn clean test. If you use clean , all the resources and compiled java code generated by maven in target directory will be cleaned and run tests freshly.

$ mvn test

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.643 s - in com.javabydeveloper.test.MathUtilTest
[INFO] Running com.javabydeveloper.test.MessageUtilTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 s - in com.javabydeveloper.test.MessageUtilTest
[INFO] Running com.javabydeveloper.test.Payment_Test
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 s - in com.javabydeveloper.test.Payment_Test
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

1.2. Run Single Test with Maven

To run single test with Maven use command like $ mvn test -Dtest=MathUtilTest  – This maven command Run only single test MathUtilTest.

$ mvn test -Dtest=MathUtilTest

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.212 s - in com.javabydeveloper.test.MathUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

1.3. Run Specific Tests

Some times you may need to run multiple specific tests. Maven command $ mvn test -Dtest=MathUtilTest,Payment_Test can be used to run specific tests.

$ mvn test -Dtest=MathUtilTest,Payment_Test

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.163 s - in com.javabydeveloper.test.MathUtilTest
[INFO] Running com.javabydeveloper.test.Payment_Test
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 s - in com.javabydeveloper.test.Payment_Test
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0

1.4. Run Test Methods

Maven surefire plugin also supports to run specific methods within test classes. Use maven command like $ mvn test -Dtest=MathUtilTest#test_add to run test methods.

$ mvn test -Dtest=MathUtilTest#test_add

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.191 s - in com.javabydeveloper.test.MathUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

You can use the Maven command $ mvn test -Dtest=MathUtilTest#test_add+test_multiply –  to run multiple methods in single test class.

$ mvn test -Dtest=MathUtilTest#test_add+test_multiply

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.191 s - in com.javabydeveloper.test.MathUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

1.5. Run all Tests in Specific package

There are several ways to run the tests cases within a specific package with Maven. You can use either full package name or can be used patterns aswell.

Maven command $ mvn test -Dtest="com/javabydeveloper/test/**" can be used to run tests in specific package. If you are using surefire plugin 2.19.1 or higher, also supports $ mvn test -Dtest="com.javabydeveloper.test.**"  

$ mvn test -Dtest="com.javabydeveloper.test.**"

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.136 s - in com.javabydeveloper.test.MathUtilTest
[INFO] Running com.javabydeveloper.test.MessageUtilTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.102 s - in com.javabydeveloper.test.MessageUtilTest
[INFO] Running com.javabydeveloper.test.Payment_Test
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 s - in com.javabydeveloper.test.Payment_Test
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

1.6. Run Tests using Patterns

Patterns also can be used to match packages or test class method names or test names. $ mvn clean test -Dtest=*UtilTest* 

$ mvn clean test -Dtest=*UtilTest*

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.test.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.238 s - in com.javabydeveloper.test.MathUtilTest
[INFO] Running com.javabydeveloper.test.MessageUtilTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 s - in com.javabydeveloper.test.MessageUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0

You also can use following commands:

$ mvn test -Dtest=*UtilTest#*add*       – to run multiple methods in multiple test classes using pattern

$ mvn test -Dtest=“com.javabydeveloper.test.*#*add*+*is*”        – to run test methods whose name contains “add” or “is” in all tests of specific package

2. Conclusion

In this quick guide, we learned how to run single test or specific unit tests using Maven with ample of examples.

You also might interested in following Maven guides:

  1. Install Maven on Windows
  2. Install Maven on Mac
  3. Install Maven on Ubuntu
  4. Import maven project into Eclipse
  5. Maven Skip Tests
  6. Maven set java version
  7. Maven run java main class

3. References

  1. Maven Document

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here