Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lightning Talk by Ted Young. What is Integration Testing?

Similar presentations


Presentation on theme: "Lightning Talk by Ted Young. What is Integration Testing?"— Presentation transcript:

1 Lightning Talk by Ted Young

2 What is Integration Testing?

3 Unit TestIntegration Test IsolationEntire Stack Unit Versus Integration Tests

4 Unit TestIntegration Test IsolationEntire Stack Inject MocksInject Implementations Unit Versus Integration Tests

5 Unit TestIntegration Test IsolationEntire Stack Inject MocksInject Implementations Verifies CodeVerifies Application Unit Versus Integration Tests

6 public void persist(Foo foo) { entityManager.persist(foo); } public List find() { return entityManager.createQuery("from Foo").getResultList(); } public List findByName(String name) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery query = cb.createQuery(Foo.class); Root root = query.from(Foo.class); query.where(cb.equal(root.get(Foo_.name), name)); return entityManager.createQuery(query).getResultList(); } Unit Tests Arent Always Best

7 External Tool (e.g. JMeter)Test Harness (e.g. JUnit) External ToolIDE, Maven, CI, etc. Integration Testing Controllers

8 External Tool (e.g. JMeter)Test Harness (e.g. JUnit) External ToolIDE, Maven, CI, etc. Script ActionsBuild Requests in Java Integration Testing Controllers

9 External Tool (e.g. JMeter)Test Harness (e.g. JUnit) External ToolIDE, Maven, CI, etc. Script ActionsBuild Requests in Java No Knowledge of ApplicationIntimate Knowledge of Application: Security System Data Model Make Use of Spring Integration Testing Controllers

10 External Tool (e.g. JMeter)Test Harness (e.g. JUnit) External ToolIDE, Maven, CI, etc. Script ActionsBuild Requests in Java No Knowledge of ApplicationIntimate Knowledge of Application: Security System Data Model Make Use of Spring Refactor = RewriteMake Use of IDE Tools Integration Testing Controllers

11 External Tool (e.g. JMeter)Test Harness (e.g. JUnit) External ToolIDE, Maven, CI, etc. Script ActionsBuild Requests in Java No Knowledge of ApplicationIntimate Knowledge of Application: Security System Data Model Make Use of Spring Refactor = RewriteMake Use of IDE Tools Errors at RuntimeErrors at Compiletime Integration Testing Controllers

12 Testing a Controller Servlet Container Controller

13 Testing a Spring MVC Controller Servlet Container Controller Spring MVC

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

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

16 Mocking DispatcherServlet DispatcherServlet

17 Mocking DispatcherServlet DispatcherServlet WebApplicationContext

18 Mocking DispatcherServlet DispatcherServlet WebApplicationContext ServletConfigServletContext

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

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

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

22 How Many Use: JSPs Velocity Freemarker Facelets View Technologies

23 @Autowired private DispatcherServlet servlet; @Autowired private SomeRepository repository; @Test public void viewTest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/view"); request.addParameter("id", "0"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); String results = response.getContentAsString().trim(); Assert.assertEquals( " Hello World! ", results); } An Example Test

24 @Test public void saveTest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); request.addParameter("name", "Ted"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); Assert.assertEquals("Ted", repository.find(1).getName()); } Prepare and Review Model

25 @Test(expected=NestedServletException.class) public void saveFailedTest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); request.addParameter("name", ""); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); } Test Validation

26 @Test(expected=NestedServletException.class) public void secureFailedTest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/secure/view"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); } Test Security

27 @Test public void secureTest() throws Exception { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken( "Ted", "password")); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/secure/view"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); String results = response.getContentAsString().trim(); Assert.assertEquals( " Hello Ted! ", results); } Test Security

28 http://tedyoung.me mail@tedyoung.me


Download ppt "Lightning Talk by Ted Young. What is Integration Testing?"

Similar presentations


Ads by Google