보라코딩
RestController test (MockMVC) 본문
SampleController.java
package com.mystudy.spring;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.IntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.sound.sampled.LineListener;
import org.apache.taglibs.standard.lang.jstl.test.beans.PublicInterface2;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mystudy.domain.SampleVO;
import com.mystudy.domain.Ticket;
import lombok.extern.log4j.Log4j;
@RestController
@RequestMapping("/sample")
@Log4j
public class SampleController {
@GetMapping(value = "/getText", produces = "text/plain; charset=UTF-8")
public String getText() {
log.info("MIME TYPE : " + MediaType.TEXT_PLAIN_VALUE);
return "안녕하세요";
}
@GetMapping(value = "/getSample",
produces = { MediaType.APPLICATION_JSON_UTF8_VALUE,
MediaType.APPLICATION_XML_VALUE})
public SampleVO getSample() {
return new SampleVO(112, "싼쵸", "바보");
}
@GetMapping(value = "/getList")
public List<SampleVO> getList(){
return IntStream.range(1, 10).mapToObj(i -> new SampleVO(i, i+"First", i+"Last"))
.collect(Collectors.toList());
}
@GetMapping(value = "/getMap")
public Map<String, SampleVO> getMap(){
Map<String, SampleVO> map = new HashMap<String, SampleVO>();
map.put("First", new SampleVO(111, "로켓", "라쿤"));
return map;
}
@GetMapping(value = "/check", params = {"height", "weight"})
public ResponseEntity<SampleVO> check(Double height, Double weight){
SampleVO vo = new SampleVO(0, "" + height, "" + weight);
ResponseEntity<SampleVO> result = null;
if (height < 150) {
result = ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(vo);
} else {
result = ResponseEntity.status(HttpStatus.OK).body(vo);
}
return result;
}
@GetMapping("/product/{cat}/{pid}")
public String[] getPath(@PathVariable("cat") String cat,
@PathVariable("pid") Integer pid) {
return new String[] {"category : " + cat, "productid : " + pid};
}
@PostMapping("/ticket")
public Ticket convert(@RequestBody Ticket ticket) {
log.info("convert.......ticket" + ticket);
return ticket;
}
}
SampleControllerTests.java
package com.mystudy.spring;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
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 com.google.gson.Gson;
import com.mystudy.domain.Ticket;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
//Test for Controller
@WebAppConfiguration
@ContextConfiguration(
{"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
@Log4j
public class SampleControllerTests {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
}
@Test
public void testConvert1() throws Exception {
Ticket ticket = new Ticket();
ticket.setTno(123);
ticket.setOwner("Admin");
ticket.setGrade("AAA");
String jsonStr = new Gson().toJson(ticket);
log.info(jsonStr);
mockMvc.perform(post("/sample/ticket")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonStr))
.andExpect(status().is(200));
}
}
'코딩 > Spring' 카테고리의 다른 글
스프링 파일 업로드 (ajax) (0) | 2023.05.14 |
---|---|
페이징처리, 검색 처리 SQL (0) | 2023.05.12 |
스프링 게시판 CRUD view (.jsp) (0) | 2023.05.12 |
button 버튼 다양하게 사용하기 (0) | 2023.05.12 |
Day94_230511_스프링 게시판 만들기 진행 중 (화면) (0) | 2023.05.11 |