JUnit Tutorial
Introduction to Junit JUnit Set up JUnit Architecture JUnit Annotations JUnit Fixtures Junit Assertions Junit Categories @Test Annotation Parameters Verification of Exceptions Ignoring tests Time out in JUnit tests Parameterizing tests Test Suite TestWatcher TemporaryFolder ExternalResource Theories in JUnit JUnit Test Runners Execution order of JUnit tests Assumptions in JUnit JUnit and Hamcrest Matchers Running JUnit tests in parallel JUnit and Maven Integration JUnit and Gradle Integration Executing Selenium tests using JUnit test framework Method interceptor and usage JUnit in Intellij IDEA JUnit in EclipseUsing maven with junit
Note – I am using Maven Surefire plug-in version 2.19.1 and JUnit version 4.12. Ensure that you also use these versions or latest ones if available. Otherwise some commands may not work.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
JUnit integrates very well with Maven. We can execute JUnit tests using Maven by various ways. In this post, we will see below topics.- Running all JUnit tests in a project using maven
- Running JUnit tests from a specific test class (using maven command and through surefire plug-in configuration )
- Running JUnit tests from specific categories (using maven command and through surefire plug-in configuration )
- Surefire plug-in configuration for running JUnit tests
1. Running all JUnit tests
Simplest way to run all JUnit tests from your project is below command. To execute all tests, you will have to ensure that class names follow standard convention.
mvn test
2. Running tests from specific JUnit test classes
If you want to run tests from specific class, you can execute below command. In below example abc is the package and SanityTests is the name of Test class.
mvn test -Dtest=abc/SanityTests
In below example, all tests from 2 classes (validations/ValidationTests and junittests/FrontEndTests) will be executed.
mvn test -Dtest=validations/ValidationTests,junittests/FrontEndTests
In below example, tests from all classes with their names ending with TestClass will be executed.
mvn test -Dtest=**/*TestClass
We can also configure test classes to be run through Maven surefire plug-in as mentioned below. To run tests from all classes, you should use below surefire configuration.
<includes>
<include>%regex[.*]</include>
</includes>
To run tests from specific classes, you should use below surefire configuration.
<includes>
<include>junittests.FrontEndTests,junittests.BackEndTests</include>
</includes>
To exclude some tests from specific classes, you should use below surefire configuration.
<includes>
<include>%regex[.*]</include>
</includes>
<excludes>
<exclude>junittests.FrontEndTests,junittests.BackEndTests</exclude>
</excludes>
You can also use maven command as shown below to run tests from all classes except specific ones. Below command will run tests from
mvn test -Dtest=!validations.ValidationTests,**/*
3. Running specific test methods in Test class
We can also run only specific test methods in a test class. In below example, only 2 tests will run from ValidationTests class – test1 and test2
mvn test -Dtest=validations/ValidationTests#test2+test1
In below example, all tests with name starting with test will be run from ValidationTests class.
mvn test -Dtest=validations/ValidationTests#test*
4. Running JUnit tests from specific categories
You can execute tests from specific categories by using groups switch. Below command will execute all tests belonging to mycategories.SanityTests category. Please ensure that surefire plug-in is configured properly as depicted below. Notice that we have included all classes in includes.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>%regex[.*]</include>
</includes>
</configuration>
mvn test -Dgroups=mycategories.SanityTests
We can also run tests from multiple categories by using below command.
mvn test -Dgroups=mycategories.SanityTests,mycategories.RegressionTests
Instead of command line, you can also run the tests from specific category by configuring the surefire plug-in as specified below. For example, in below configuration, we have specified that tests from SanityTests and RegressionTests categories should be executed.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>%regex[.*]</include>
</includes>
<groups>mycategories.SanityTests,mycategories.RegressionTests</groups>
</configuration>
</plugin>
Sometimes, you might need to run tests from all groups except few ones. In that case, you can use below configuration. Notice ! in front of the category name. ! tells surefire plugin to select tests from all groups except mycategories.SanityTests
<configuration>
<includes>
<include>%regex[.*]</include>
</includes>
<groups>!mycategories.SanityTests</groups>
</configuration>
You can also execute the same command from command line as mentioned below.
mvn test -Dgroups=!mycategories.SanityTests
Alternatively, we can use excludedgroups tag.
<configuration>
<includes><include>%regex[.*]</include></includes>
<excludedGroups>mycategories.SanityTests</excludedGroups>
</configuration>
5. More Surefire plugin configuration for running JUnit tests
In this section, let us look at few more configurations of Surefire plug-in.- Skipping tests
- Running tests in parallel
- Using custom listener and reporter
- Working with multiple modules
Skipping tests
Sometimes, we may need to skip the tests. In that scenario, you can use below command.
mvn test -DskipTests=true
Or you can skip the tests by configuring the surefire plug-in as well as shown below.
<configuration>
<skipTests>true</skipTests>
</configuration>
Web development and Automation testing
solutions delivered!!