content: package com.codesoom.demo.controllers;
  
  import com.codesoom.demo.application.AuthenticationService;
  import com.codesoom.demo.errors.LoginFailException;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.http.MediaType;
  import org.springframework.test.web.servlet.MockMvc;
  
  import static org.hamcrest.Matchers.containsString;
  import static org.mockito.BDDMockito.given;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  @WebMvcTest(SessionController.class)
  class SessionControllerTest {
      @Autowired
      private MockMvc mockMvc;
  
      @MockBean
      private AuthenticationService authenticationService;
  
      @BeforeEach
      void setUp() {
          given(authenticationService.login("tester@example.com", "test"))
                  .willReturn("a.b.c");
  
          given(authenticationService.login("badguy@example.com", "test"))
                  .willThrow(new LoginFailException("badguy@example.com"));
  
          given(authenticationService.login("tester@example.com", "xxx"))
                  .willThrow(new LoginFailException("tester@example.com"));
      }
  
      @Test
      void loginWithRightEmailAndPassword() throws Exception {
          mockMvc.perform(
                  post("/session")
                          .contentType(MediaType.APPLICATION_JSON)
                          .content("{\"email\":\"tester@example.com\"," +
                                  "\"password\":\"test\"}")
          )
                  .andExpect(status().isCreated())
                  .andExpect(content().string(containsString(".")));
      }
  
      @Test
      void loginWithWrongEmail() throws Exception {
          mockMvc.perform(
                  post("/session")
                          .contentType(MediaType.APPLICATION_JSON)
                          .content("{\"email\":\"badguy@example.com\"," +
                                  "\"password\":\"test\"}")
          )
                  .andExpect(status().isBadRequest());
      }
  
      @Test
      void loginWithWrongPassword() throws Exception {
          mockMvc.perform(
                  post("/session")
                          .contentType(MediaType.APPLICATION_JSON)
                          .content("{\"email\":\"tester@example.com\"," +
                                  "\"password\":\"xxx\"}")
          )
                  .andExpect(status().isBadRequest());
      }
  }
  