| 1 | |
---|
| 2 | {{{ |
---|
| 3 | MockMvcBuilder.standaloneMvcSet(new TestController()).build() |
---|
| 4 | .perform(get("/form")) |
---|
| 5 | .andExpect(status().isOk()) |
---|
| 6 | .andExpect(content().type("text/plain")) |
---|
| 7 | .andExpect(content().string("hello world")); |
---|
| 8 | |
---|
| 9 | 1. NullpointerException이 발생한다. |
---|
| 10 | |
---|
| 11 | TestController controller = new TestController(); |
---|
| 12 | MockHttpServletRequest req = new MockHttpRequest(); |
---|
| 13 | MockHttpServletResponse res = new MockHttpResponse(); |
---|
| 14 | ModelAndView mav = controller.form(req,res); |
---|
| 15 | assertThat(res.getStatus(),is(200)); |
---|
| 16 | |
---|
| 17 | 1. 두 매개변수는 @MVC에서 자주 사용하지 않는 매개변수이다. |
---|
| 18 | ==> @RequestMapping |
---|
| 19 | @ModelAttribute |
---|
| 20 | Model |
---|
| 21 | |
---|
| 22 | 2. 결과타입은 ModelAndView로 가정한다. |
---|
| 23 | ==> String (View 이름만 반환) |
---|
| 24 | void도 많다. |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | 단위테스트 |
---|
| 28 | 모든 객체의 Mock객체를 만들어서 컨트롤러에 주입테스트 |
---|
| 29 | |
---|
| 30 | 통합테스트 |
---|
| 31 | 테스트용 ApplicationContext만들어 비 주입 기능을 사용해야 한다. |
---|
| 32 | ==> TestContext라는 기능으로 지원하고 |
---|
| 33 | ApplicationContext 공유 기능을 제공한다. |
---|
| 34 | |
---|
| 35 | ApplicationContext를 한 번만 만들어서 TestContext에 캐시하여 사용한다. |
---|
| 36 | |
---|
| 37 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
| 38 | @contextConfiguration("/testContext.xml") |
---|
| 39 | public class TestClassA{ |
---|
| 40 | |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | ==>@RunWith |
---|
| 44 | @ContextConfiguration |
---|
| 45 | @Autowired |
---|
| 46 | @Inject |
---|
| 47 | |
---|
| 48 | Spring-Test-MVC 와 TestContext 연동 |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | @Autowired ApplicationContext context; --> MockMvc |
---|
| 52 | |
---|
| 53 | MockMvc mockMvc = MockMvcBuilders.applicationContextMvcSetup(context).configureWasRootDir("src/test/webapp",false).build(); |
---|
| 54 | |
---|
| 55 | Spring-Test-MVC는 ApplicationContext 타입의 객체로 MockMvc 객체를 마드는 메서드를 제공하지 않는다. |
---|
| 56 | WebApplicatioinContext 타입은 가능하다. |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | }}} |