Unit Test Controllers with Spring MVC Testby Pigbrain

  • 스프링 MVC Test 프로젝트는 컨트롤러를 테스트 할 수 있는 기능을 제공한다

Getting Ready

Application Setup

  • pom.xml에 디펜던시를 추가한다
  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-test</artifactId>
  4. <version>3.2.3.RELEASE</version>
  5. </dependency>
  6. <!-- This is for mocking the service -->
  7. <dependency>
  8. <groupId>org.mockito</groupId>
  9. <artifactId>mockito-all</artifactId>
  10. <version>1.9.5</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <!-- Optional -->
  14. <dependency>
  15. <groupId>org.hamcrest</groupId>
  16. <artifactId>hamcrest-core</artifactId>
  17. <version>1.3</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.hamcrest</groupId>
  22. <artifactId>hamcrest-library</artifactId>
  23. <version>1.3</version>
  24. <scope>test</scope>
  25. </dependency>

Reference Class

  • 컨트롤러 테스트를 위하여 아래의 IndexController를 생성하였다
  1. @Controller
  2. public class IndexController {
  3. public static final String PAGE_INDEX = "index";
  4. public static final String PAGE_SHOW = "show";
  5. @Autowired
  6. SampleService sampleService;
  7. @RequestMapping(value = "/", method = RequestMethod.GET)
  8. public ModelAndView index() {
  9. return new ModelAndView(PAGE_INDEX, "signupForm", new SignupForm());
  10. }
  11. @RequestMapping(value = "/create", method = RequestMethod.POST)
  12. public String create(Model model, @Valid SignupForm signupForm, BindingResult result) {
  13. String returnPage = PAGE_INDEX;
  14. if (!result.hasErrors()) {
  15. try {
  16. model.addAttribute("signupForm", sampleService.saveFrom(signupForm));
  17. returnPage = PAGE_SHOW;
  18. } catch (InvalidUserException e) {
  19. model.addAttribute("page_error", e.getMessage());
  20. }
  21. }
  22. return returnPage;
  23. }
  24. }

Create The Spring MVC Test Class

  • Mockito를 사용하여 Service객체를 Mock처리 한다
  1. public class IndexControllerTest {
  2. @Mock
  3. private SampleService sampleService;
  4. @InjectMocks
  5. private IndexController indexController;
  6. private MockMvc mockMvc;
  7. @Before
  8. public void setup() {
  9. // Process mock annotations
  10. MockitoAnnotations.initMocks(this);
  11. // Setup Spring test in standalone mode
  12. this.mockMvc = MockMvcBuilders.standaloneSetup(indexController).build();
  13. }
  14.  
  15. @Test
  16. public void testCreateSignupFormInvalidUser() throws Exception {
  17. when(sampleService.saveFrom(any(SignupForm.class)))
  18. .thenThrow(new InvalidUserException("For Testing"));
  19. this.mockMvc.perform(post("/create")
  20. .param("email", "mvcemail@test.com")
  21. .param("firstName", "mvcfirst")
  22. .param("lastName", "mvclastname"))
  23. .andExpect(status().isOk())
  24. .andExpect(forwardedUrl(IndexController.PAGE_INDEX))
  25. .andExpect(model().attributeExists("page_error"));
  26. }
  27. }

원문

  • https://www.luckyryan.com/2013/08/24/unit-test-controllers-spring-mvc-test/
Published 03 December 2016