[스프링 부트] chapter 12. 테스트 유틸

테스트 유틸

OutputCapture

로그를 포함한 모든 Output을 캡쳐할 수 있다.

@Runwith(SpringRunner.class)
@WebTestMvc(SampleController.class)
public class SampleControllerTest {
    
    @Rule
    public OutputCapture outputCapture = new OutputCapture();

    @MockBean
    SampleService mockSampleService;

    @Autowired
    MockMvc mockMvc;

    @Test
    public void hello() throws Exception {
        when(mockSampleService.getName()).thenReturn("shinypillar");

        mockMvc.perform(get("/hello"))
                .andExpect(content().string("hello shinypillar");

        assertThat(outputCapture.toString())
                .contains("test")
                .contains("test2");
    }
}