001package com.lucidtech.puttingalltogether.samples;
002
003import org.junit.jupiter.api.Test;
004import org.springframework.beans.factory.annotation.Autowired;
005import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
006import org.springframework.boot.test.context.SpringBootTest;
007import org.springframework.test.web.servlet.MockMvc;
008
009import static org.hamcrest.Matchers.containsString;
010import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
011import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
012import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
013import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
014
015@SpringBootTest
016@AutoConfigureMockMvc
017//Another useful approach is to not start the server at all but to test only the layer below that, where Spring handles
018// the incoming HTTP request and hands it off to your controller. That way, almost of the full stack is used,
019// and your code will be called in exactly the same way as if it were processing a real HTTP request
020// but without the cost of starting the server. To do that, use Spring’s MockMvc and ask for that to be injected for
021// you by using the @AutoConfigureMockMvc annotation on the test case.
022class StartFullSpringContextButWithoutServerMockMvcHttpRequestIT {
023    @Autowired
024    private MockMvc mockMvc;
025
026    @Test
027    public void shouldReturnDefaultMessage() throws Exception {
028        this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
029                .andExpect(content().string(containsString("World")));
030    }
031}