Skip to content

Instantly share code, notes, and snippets.

@up1
Created January 9, 2013 09:49
Show Gist options
  • Save up1/4491980 to your computer and use it in GitHub Desktop.
Save up1/4491980 to your computer and use it in GitHub Desktop.
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
import static org.junit.Assert.assertTrue;
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.client.MockRestServiceServer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
import up1.demo.config.TestWebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { TestWebConfig.class })
public class FeedControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getInfo() throws Exception {
this.mockMvc.perform(get("/feed/1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.title").value("Somkiat"));
}
@Test
public void callFromRestClient() throws Exception {
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(anything());
mockServer.expect(anything());
mockServer.expect(requestTo("/feed/1")).andRespond(withSuccess("Hello world", MediaType.APPLICATION_JSON));
try {
mockServer.verify();
} catch (AssertionError error) {
assertTrue(error.getMessage(), error.getMessage().contains("0 out of 3 were executed"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment