1 / 28

Integration Testing Spring Controllers

Integration Testing Spring Controllers. Lightning Talk by Ted Young. What is Integration Testing?. Unit Versus Integration Tests. Unit Versus Integration Tests. Unit Versus Integration Tests. Unit Tests Aren’t Always Best. public void persist(Foo foo) { entityManager .persist (foo ); }

Download Presentation

Integration Testing Spring Controllers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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

More Related