280 likes | 383 Views
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 ); }
E N D
Integration Testing Spring Controllers Lightning Talk by Ted Young
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(); • }
Testing a Controller Controller Servlet Container
Testing a Spring MVC Controller Controller Spring MVC Servlet Container
Testing a Spring MVC Controller Controller Spring MVC Transactions Request Mapping View Resolution Servlet Container
Testing a Spring MVC Controller Controller Spring MVC Transactions Request Mapping View Resolution DispatcherServlet Servlet Container
Mocking DispatcherServlet DispatcherServlet
Mocking DispatcherServlet DispatcherServlet WebApplicationContext
Mocking DispatcherServlet DispatcherServlet WebApplicationContext ServletConfig ServletContext
Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration(locations="classpath:spring.xml") • publicclassSomeControllerTests { • ... • }
Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration( locations="classpath:spring.xml", loader=MockWebApplicationContextLoader.class) • publicclassSomeControllerTests { • ... • }
Spring and JUnit • @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration( locations="classpath:spring.xml", loader=MockWebApplicationContextLoader.class) • @MockWebApplication( name="some-controller",webapp="/src/main/webapp") • publicclassSomeControllerTests { • ... • }
View Technologies • How Many Use: • JSPs • Velocity • Freemarker • Facelets
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); • }
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()); • }
Test Validation • @Test(expected=NestedServletException.class) • publicvoidsaveFailedTest() throws Exception { • MockHttpServletRequestrequest = • newMockHttpServletRequest("POST", "/"); • request.addParameter("name", ""); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • }
Test Security • @Test(expected=NestedServletException.class) • publicvoidsecureFailedTest() throwsException { • MockHttpServletRequestrequest = • newMockHttpServletRequest("GET", "/secure/view"); • MockHttpServletResponseresponse = • newMockHttpServletResponse(); • servlet.service(request, response); • }
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); • }
Please Visit My Site http://tedyoung.me mail@tedyoung.me