Integration Testing Spring Controllers - PowerPoint PPT Presentation

nickan
integration testing spring controllers n.
Skip this Video
Loading SlideShow in 5 Seconds..
Integration Testing Spring Controllers PowerPoint Presentation
Download Presentation
Integration Testing Spring Controllers

play fullscreen
1 / 28
Download Presentation
Integration Testing Spring Controllers
133 Views
Download Presentation

Integration Testing Spring Controllers

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Integration Testing Spring Controllers Lightning Talk by Ted Young

  2. What is Integration Testing?

  3. Unit Versus Integration Tests

  4. Unit Versus Integration Tests

  5. Unit Versus Integration Tests

  6. Unit Tests Aren’t Always Best • publicvoid persist(Foo foo) { • entityManager.persist(foo); • } • public List<Foo> find() { • returnentityManager • .createQuery("from Foo").getResultList(); • } • public List<Foo> findByName(String name) { • CriteriaBuildercb = entityManager.getCriteriaBuilder(); • CriteriaQuery<Foo> query = cb.createQuery(Foo.class); • Root<Foo> root = query.from(Foo.class); • query.where(cb.equal(root.get(Foo_.name), name)); • returnentityManager.createQuery(query).getResultList(); • }

  7. Integration Testing Controllers

  8. Integration Testing Controllers

  9. Integration Testing Controllers

  10. Integration Testing Controllers

  11. Integration Testing Controllers

  12. Testing a Controller Controller Servlet Container

  13. Testing a Spring MVC Controller Controller Spring MVC Servlet Container

  14. Testing a Spring MVC Controller Controller Spring MVC Transactions Request Mapping View Resolution Servlet Container

  15. Testing a Spring MVC Controller Controller Spring MVC Transactions Request Mapping View Resolution DispatcherServlet Servlet Container

  16. Mocking DispatcherServlet DispatcherServlet

  17. Mocking DispatcherServlet DispatcherServlet WebApplicationContext

  18. Mocking DispatcherServlet DispatcherServlet WebApplicationContext ServletConfig ServletContext

  19. Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration(locations="classpath:spring.xml") • publicclassSomeControllerTests { • ... • }

  20. Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration( locations="classpath:spring.xml", loader=MockWebApplicationContextLoader.class) • publicclassSomeControllerTests { • ... • }

  21. Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration( locations="classpath:spring.xml", loader=MockWebApplicationContextLoader.class) • @MockWebApplication( name="some-controller",webapp="/src/main/webapp") • publicclassSomeControllerTests { • ... • }

  22. View Technologies • How Many Use: • JSPs • Velocity • Freemarker • Facelets

  23. An Example Test • @Autowired • privateDispatcherServletservlet; • @Autowired • privateSomeRepositoryrepository; • @Test • publicvoidviewTest() throwsException{ • MockHttpServletRequestrequest = • newMockHttpServletRequest("GET", "/view"); • request.addParameter("id", "0"); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • String results = response.getContentAsString().trim(); • Assert.assertEquals( • "<html><body>Hello World!</body></html>", • results); • }

  24. Prepare and Review Model • @Test • publicvoidsaveTest() throws Exception { • MockHttpServletRequestrequest = • newMockHttpServletRequest("POST", "/"); • request.addParameter("name", "Ted"); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • Assert.assertEquals("Ted", repository.find(1).getName()); • }

  25. Test Validation • @Test(expected=NestedServletException.class) • publicvoidsaveFailedTest() throws Exception { • MockHttpServletRequestrequest = • newMockHttpServletRequest("POST", "/"); • request.addParameter("name", ""); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • }

  26. Test Security • @Test(expected=NestedServletException.class) • publicvoidsecureFailedTest() throwsException { • MockHttpServletRequestrequest = • newMockHttpServletRequest("GET", "/secure/view"); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • }

  27. Test Security • @Test • publicvoidsecureTest() throwsException { • SecurityContextHolder.getContext().setAuthentication( • newUsernamePasswordAuthenticationToken( • "Ted", "password")); • MockHttpServletRequestrequest = • newMockHttpServletRequest("GET", "/secure/view"); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • String results = response.getContentAsString().trim(); • Assert.assertEquals( • "<html><body>Hello Ted!</body></html>", • results); • }

  28. Please Visit My Site http://tedyoung.me mail@tedyoung.me