package com.rest.api.controller; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; 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.*; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc @Transactional public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void helloworldString() throws Exception { mockMvc.perform(get("/helloworld/string")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType("text/plain;charset=UTF-8")) .andExpect(content().string("helloworld")); } @Test public void helloworldJson() throws Exception { mockMvc.perform(get("/helloworld/json")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=utf-8")) .andExpect(jsonPath("$.message").value("helloworld")); } @Test public void helloworldPage() throws Exception { mockMvc.perform(get("/helloworld/page")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType("text/html;charset=UTF-8")) .andExpect(view().name("helloworld")) .andExpect(content().string("helloworld")); } }