Created
February 25, 2017 20:34
-
-
Save javaeeeee/0a87938ab0101f180d3dda0126300fa2 to your computer and use it in GitHub Desktop.
A test for a Spring MVC REST Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javaeeeee.springrest.controllers; | |
import org.junit.Test; | |
import org.junit.Before; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.context.web.WebAppConfiguration; | |
import org.springframework.test.web.servlet.MockMvc; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
import org.springframework.web.context.WebApplicationContext; | |
@RunWith(SpringRunner.class) | |
@WebAppConfiguration | |
@ContextConfiguration("classpath:test-dispatcher-servlet.xml") | |
public class HelloControllerTest { | |
@Autowired | |
private WebApplicationContext webApplicationContext; | |
private MockMvc mockMvc; | |
@Before | |
public void setUp() { | |
this.mockMvc = MockMvcBuilders | |
.webAppContextSetup(this.webApplicationContext) | |
.build(); | |
} | |
@Test | |
public void testGetGreeting() throws Exception { | |
this.mockMvc.perform(get("/hello")) | |
.andExpect(status().isOk()) | |
.andExpect(content().string("Hello Spring World!")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment