Skip to content

Instantly share code, notes, and snippets.

@jbrackett
Last active August 29, 2015 13:56
Show Gist options
  • Save jbrackett/8909718 to your computer and use it in GitHub Desktop.
Save jbrackett/8909718 to your computer and use it in GitHub Desktop.
HelloWorldSpring4Test
package com.hello.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.hello.AppConfig;
import com.hello.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { AppConfig.class, WebConfig.class })
public class HelloControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void helloTest() throws Exception {
String name = "Test";
mockMvc
.perform(get("/hello/{name}", name).accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(
content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().string(name + " from the server"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment