Junit5 Test Rest With Springboot And Mockmvc

[ ]

In order to run tests of REST Api with a minimal Spring context in Junit5. BDD Mockito style.

Light test configuration

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;

/**
 * Light Spring context to test only REST.
 */
@Configuration
@ComponentScan({"com.package.to.rest"})
public class MyRestTestConfig {

}

Test class

//...
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.BDDMockito.willThrow;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(value = MyResourceRestController.class, secure = false)
@SpringJUnitWebConfig(MyRestTestConfig.class)
class MyResourceRestTest {

    @Autowired
    private MockMvc mvc;
    
    @MockBean
    private SomeInjectedService someInjectedService;
    

    @Test
    void updateResource() throws Exception {

        mvc.perform(post("/api/myresource/{resourceId}/param/{param}",
                1L, "paramValue"))
                .andExpect(status().isOk());
    }
}

Source

Written on May 23, 2018
Licence Creative Commons
Ce(tte) œuvre est mise à disposition selon les termes de la Licence Creative Commons Attribution 4.0 International.