Java Programming Tutorials

Java programming tutorials with many code examples!

Spring Rest Controller Testing


How to test Spring Rest Controller?


Sample Gradle build script to run test:

buildscript {
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'spring-rest-controller-testing'
    version =  '0.1.0'

repositories {

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // mandatory dependencies for using Spock
    compile "org.codehaus.groovy:groovy-all:2.4.1"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"


task wrapper(type: Wrapper) {
    gradleVersion = '2.4'

Domain object that will be converted to JSON when returned from REST Controller:

package com.farenda.solved;

public class User {

    private final Long id;
    private final String name;

    public User(Long id, String name) { = id; = name;

    public Long getId() {
        return id;

    public String getName() {
        return name;

A Repository/DAO/Service that will provide domain objects for our REST controller:

package com.farenda.solved;

import java.util.List;

// An interface is enough as we don't want to test its logic.
public interface UserRepository {
    List<User> getAll();

REST Controller that we want to test:

package com.farenda.solved;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

public class JavaSolvedController {

    private UserRepository userRepository;

    @RequestMapping(method = GET, value = "/user",
                    produces = APPLICATION_JSON_VALUE)
    public List<User> listUsers() {
        return userRepository.getAll();

Finally Spock test using Spring MVC Test, which is part of spring-test since Spring 3.2:

package com.farenda.solved

import org.springframework.http.MediaType
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import spock.lang.Specification
import spock.lang.Subject

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*

// No @ContextConfiguration with Spring MVC Test!
class JavaSolvedControllerTest extends Specification {

    // Don't want to call real service, so use mock
    def repository = Mock(UserRepository)
    // Init controller with mock:
    def @Subject api = new JavaSolvedController(userRepository: repository)

    // Let Spring MVC Test process the controller:
    def mockMvc = MockMvcBuilders.standaloneSetup(api).build()

    def 'should call rest controller'() {
        // Try different URL or Method to see what will happen!
        def result = mockMvc.perform(get('/user'))

        // Expect 1 call to repository to get users:
        1 * repository.getAll() >> [new User(1, 'franek')]

Nice thing is that we don’t have to create whole Spring Application Context just to test simple Rest Controller. Spring MVC Test will process controller’s annotations and allow to easily test every its aspect!

Was it helpful? Share!