Last active
August 29, 2015 13:56
-
-
Save jbrackett/8909718 to your computer and use it in GitHub Desktop.
HelloWorldSpring4Test
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.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