/*
 * Spring-boot Examples
 *
 * Copyright (c) 2021. Elex. All Rights Reserved.
 * https://www.elex-project.com/
 */

package kr.pe.elex.examples;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.when;
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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Mockito를 사용해서 테스트용 서비스를 모킹한다.
 */
@WebMvcTest(MyController.class)
class ControllerTest2 {
	@Autowired
	private MockMvc mockMvc;

	@MockBean
	private MyService service;

	@BeforeEach
	void beforeEach() {
		Mockito.when(service.sayHello())
				.thenReturn("Hello, Mock");
	}

	@Test
	void greetingsTest() throws Exception {
		this.mockMvc.perform(get("/greetings"))
				.andExpect(status().isOk())
				.andExpect(content().string(containsString("Hello, Mock")))
				.andDo(print());
	}

}
