lightning talk by ted young. what is integration testing?
TRANSCRIPT
![Page 1: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/1.jpg)
Lightning Talk by Ted Young
Integration Testing Spring Controllers
![Page 2: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/2.jpg)
What is Integration Testing?
![Page 3: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/3.jpg)
Unit Test Integration Test
Isolation Entire Stack
Unit Versus Integration Tests
![Page 4: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/4.jpg)
Unit Test Integration Test
Isolation Entire Stack
Inject Mocks Inject Implementations
Unit Versus Integration Tests
![Page 5: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/5.jpg)
Unit Test Integration Test
Isolation Entire Stack
Inject Mocks Inject Implementations
Verifies Code Verifies Application
Unit Versus Integration Tests
![Page 6: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/6.jpg)
public void persist(Foo foo) {entityManager.persist(foo);
}
public List<Foo> find() {return entityManager
.createQuery("from Foo").getResultList();}
public List<Foo> findByName(String name) {CriteriaBuilder cb = 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));return entityManager.createQuery(query).getResultList();
}
Unit Tests Aren’t Always Best
![Page 7: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/7.jpg)
External Tool (e.g. JMeter) Test Harness (e.g. JUnit)
External Tool IDE, Maven, CI, etc.
Integration Testing Controllers
![Page 8: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/8.jpg)
External Tool (e.g. JMeter) Test Harness (e.g. JUnit)
External Tool IDE, Maven, CI, etc.
Script Actions Build Requests in Java
Integration Testing Controllers
![Page 9: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/9.jpg)
External Tool (e.g. JMeter) Test Harness (e.g. JUnit)
External Tool IDE, Maven, CI, etc.
Script Actions Build Requests in Java
No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring
Integration Testing Controllers
![Page 10: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/10.jpg)
External Tool (e.g. JMeter) Test Harness (e.g. JUnit)
External Tool IDE, Maven, CI, etc.
Script Actions Build Requests in Java
No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring
Refactor = Rewrite Make Use of IDE Tools
Integration Testing Controllers
![Page 11: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/11.jpg)
External Tool (e.g. JMeter) Test Harness (e.g. JUnit)
External Tool IDE, Maven, CI, etc.
Script Actions Build Requests in Java
No Knowledge of Application Intimate Knowledge of Application:• Security System• Data Model• Make Use of Spring
Refactor = Rewrite Make Use of IDE Tools
Errors at Runtime Errors at Compiletime
Integration Testing Controllers
![Page 12: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/12.jpg)
Testing a Controller
Servlet Container
Controller
![Page 13: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/13.jpg)
Testing a Spring MVC Controller
Servlet Container
Controller
Spring MVC
![Page 14: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/14.jpg)
Testing a Spring MVC Controller
Servlet Container
Controller
Spring MVC
View Resolution
Transactions Request Mapping
![Page 15: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/15.jpg)
Testing a Spring MVC Controller
Servlet Container
Controller
Spring MVC
View Resolution
Transactions Request Mapping
DispatcherServlet
![Page 16: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/16.jpg)
Mocking DispatcherServlet
DispatcherServlet
![Page 17: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/17.jpg)
Mocking DispatcherServlet
DispatcherServlet
WebApplicationContext
![Page 18: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/18.jpg)
Mocking DispatcherServlet
DispatcherServlet
WebApplicationContext
ServletConfig ServletContext
![Page 19: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/19.jpg)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring.xml")
public class SomeControllerTests {
...
}
Spring and JUnit
![Page 20: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/20.jpg)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring.xml",
loader=MockWebApplicationContextLoader.class)
public class SomeControllerTests {
...
}
Spring and JUnit
![Page 21: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/21.jpg)
@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
![Page 22: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/22.jpg)
How Many Use:
• JSPs
• Velocity
• Freemarker
• Facelets
View Technologies
![Page 23: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/23.jpg)
@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(
"<html><body>Hello World!</body></html>",
results);
}
An Example Test
![Page 24: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/24.jpg)
@Testpublic 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
![Page 25: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/25.jpg)
@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
![Page 26: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/26.jpg)
@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
![Page 27: Lightning Talk by Ted Young. What is Integration Testing?](https://reader035.vdocuments.site/reader035/viewer/2022062307/5518c43c550346b31f8b56f0/html5/thumbnails/27.jpg)
@Testpublic 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("<html><body>Hello Ted!</body></html>",results);
}
Test Security