환경


  • JDK 1.8
  • Apache Maven 3.3.9
  • Spring Boot 1.3.1.RELEASE

Code


package com.wonzopein;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wonzopein.domain.base.User;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.List;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
@WebAppConfiguration
@ActiveProfiles("dev")
public class RestApiUserTests {

	final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	WebApplicationContext webApplicationContext;

	private MockMvc mockMvc;

	@Autowired
	ObjectMapper jacksonMapper;

	@Before
	public void setUp() {
		mockMvc = MockMvcBuilders
					.webAppContextSetup(webApplicationContext)
					.build();
	}

	@Test
	public void selectUsers() throws Exception {

		mockMvc.perform(get("/api/users"))
				.andDo( result -> {
					log.debug("----- User -----");
					String resultJson = result.getResponse().getContentAsString();
					log.debug(resultJson);
					List users = jacksonMapper.readValue(resultJson, new TypeReference>() {});
					users.forEach(user -> log.debug(user.toString()));
				})
				.andExpect(status().isOk());
	}
}


'Framework > Spring' 카테고리의 다른 글

Spring Boot - Mockup REST Test  (0) 2016.01.12
Spring에서 Json 활용방법  (0) 2013.09.30