JUnit 4 Aggregating Tests in Suites

1. Overview

In this guide, we will learn how to run multiple Test Classes at a time. Using Suite as a runner allows you to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite() method. To use it, annotate a class with @RunWith(Suite.class) and @SuiteClasses(TestClass1.class, ...). When you run this class, it will run all the tests in all the suite classes.
Let's create a simple maven project and demonstrate usage of Suite.

2. Create Simple Maven Project

Let's create a simple maven project. Create simple maven project by executing below command:
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application
After the maven project builds success, maven will create a default folder structure.

3. Project Packaging Structure

  • src/main/java – Java Source code packages and classes
  • src/main/resources – NON-Java Resources, such as property files and Spring configuration
Test
  • src/test/java – Test Source code packages and classes
  • src/test/resources – NON-Java Resources, such as property files and Spring configuration
Let's create package and classes as per below packaging structure.
── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── javadevelopersguide
    │   │           └── junit
    |   │               └── Login.java
    |   │               └── LoginService.java
    |   │               └── User.java
    |   │               └── UserService.java
    │   ├── resources
    └── test
        ├── java
        │   └── com
        │       └── javadevelopersguide
        │           └── junit
        │               └── LoginServiceTest.java
        │               └── UserServiceTest.java
        │               └── SuiteTest.java
        └── resources

3. Update JUnit Dependencies in pom.xml File

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
</dependency>
Let's create a login service and user service features, then test both the features as a suite.

4. Create LoginService.java and LoginServiceTest.java

4.1 LoginService.java

public class Login {
 private String userName;
 private String password;
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
}

package com.developersguide.junit.suites;

public class LoginService {
 public boolean authenticate(String userName, String password){
  if("demo".equals(userName) && "demo".equals(password)){
   return true;
  }
  return false;
 }
}

4.2 LoginServiceTest.java

package com.developersguide.junit.suites;
import static org.junit.Assert.assertTrue;

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

public class LoginServiceTest {
 @Test
 public void authenticateSuccessTest(){
  LoginService login = new LoginService();
  assertTrue("authenticate failed", login.authenticate("demo", "demo"));
 }
 
 @Test
 public void authenticateFailTest(){
  LoginService login = new LoginService();
  assertFalse("authenticate failed", login.authenticate("demo1", "demo"));
 }
}

Output

5. Create UserService.java and UserServiceTest.java

5.1 UserService.java

public class User {
 private int id;
 private String name;
 public User(int id,String name){
  this.id = id;
  this.name = name;
 }
}
package com.developersguide.junit.suites;

import java.util.ArrayList;
import java.util.List;

public class UserService {
 private List<User> list = new ArrayList<User>();
 
 public User createUser(User user){
  list.add(user);
  return user;
 }
 
 public List<User> getUsers(){
  return this.list;
 }
 
 public void deleteUser(User user){
  list.remove(user);
 }
}

5.2 UserServiceTest.java

package com.developersguide.junit.suites;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;

public class UserServiceTest {
 private User user;
 private UserService userService = new UserService();
 
 @Before
 public void setup(){
  user = new User(100, "ramesh");
 }
 
 @Test
 public void userServiceTest(){
  userService.createUser(user);
  List<User> users = userService.getUsers();
  assertEquals(1, users.size());
  userService.deleteUser(user);
  users = userService.getUsers();
  assertEquals(0, users.size());
 }
 
}

6. Create Test Suite

package com.developersguide.junit.suites;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  LoginServiceTest.class,
  UserServiceTest.class,
})
public class SuiteTest {

}

7. Conclusion

In this guide, we have learned the how to create Test Suite for multiple Test Classes by creating a maven project. The source code available for this guide on GitHub. On GitHub, there is a multi-module project and this getting started project is a module of it.
GitHub Repository: JUnit Developers Guide


Comments