Testing Spring Application using JUnit
How to test a Spring Application using JUnit? Explained with example test class.

Unit Testing

JUnit is a widely used unit testing framework in Java. TestNG is another popular testing framework in Java.

Problem

It easy to do a unit testing of simple Java application where object life cycle is manually managed. In case of Spring applications, object life cycle is primarily managed by itself. We have to manually initiate Spring Application Context to test it properly. This blog post explains how to test a spring application easily using @RunWith annotation.

Maven Dependencies

To start with, we need following dependencies added to the pom.xml file. Or add these dependencies into build.gradle or build.sbt, if you are using gradle or sbt respectively.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.0.3.RELEASE</version>
  <scope>test</scope>
</dependency>

Example Code

Following example code shows how to easily do JUnit testing of Spring application.

package com.sakthipriyan.example.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

// Specify how to run this test class.
@RunWith(SpringJUnit4ClassRunner.class)
// Provide the spring configuration file in the classpath.
@ContextConfiguration("classpath:spring.xml")
public class ExampleServiceImplTest {

    // Autowire the Class that has to be tested.
    @Autowired
    private ExampleService exampleService;

    // Here test a sum method in the ExampleService.
    @Test
    public void testSum() {
        int sum = exampleService.sum(12,13);
        assertEquals(25, sum);
    }

}

Explanation

  • Using @RunWith(SpringJUnit4ClassRunner.class) annotation modifies how the test class is run.
  • @RunWith will run test with given class rather than the default JUnit implementation.
  • SpringJUnit4ClassRunner class extends JUnit's BlockJUnit4ClassRunner to provide Spring TestContext Framework.
  • @ContextConfiguration("classpath:spring.xml") specifies how to configure the Spring Application Context. In this example spring.xml is the spring config file at the root of the application classpath.
  • It is better to use the application's spring config, rather than say a separate spring-test.xml.
  • But in practice, most of time people end up using different config files for testing.
  • Once required configurations are done, yeah just add two @annotation, we are ready to test the Spring dependency injected application, using JUnit.
  • Here, we have tested a very complex problem of adding two number. Just kidding.

Share

Great!! You read till this point, just go ahead and share this post to your followers, collegues and friends. Thanks!

About Author

Sakthi Priyan H
Passionate Programmer

  • I am passionate about building excellent teams, processes and systems.
  • Primarily I use Java, Scala and Python for building various systems and tools.
  • Building API services, Big data processing and Machine Learning systems in Crayon Data.
  • Also, interested in Golang and building web apps using Javascript ecosystem.
  • I wrote my first program in BASIC in 1998, Passionate about computers since then.