001package com.lucidtech.puttingalltogether.samples;
002
003import com.lucidtech.puttingalltogether.controller.GreetingController;
004import org.junit.jupiter.api.Test;
005import org.springframework.beans.factory.annotation.Autowired;
006import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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@WebMvcTest(GreetingController.class)
016//In this test, the full Spring application context is started but without the server.
017// We can narrow the tests to only the web layer by using @WebMvcTest
018//In this test, Spring Boot instantiates only the web layer rather than the whole context.
019// In an application with multiple controllers, you can even ask for only one to be instantiated by using,
020// for example, @WebMvcTest(GreetingController.class).
021class StartSpringContextWithSpecificClassAndWithWebLayerOnlyButWithoutServerTest {
022    @Autowired
023    private MockMvc mockMvc;
024
025//    @MockBean
026//    private GreetingService service;
027
028    @Test
029    public void greetingShouldReturnMessageFromService() throws Exception {
030//        when(service.greet()).thenReturn("Hello, Mock");
031        this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
032                .andExpect(content().string(containsString("World")));
033    }
034}