Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Testing Web Apps with Spring Framework 3.2

1,196 views

Published on

Published in: Software
  • Be the first to comment

Testing Web Apps with Spring Framework 3.2

  1. 1. Testing Web Apps with Spring Framework 3.2 Sam Brannen Swiftmind Rossen Stoyanchev VMware
  2. 2. Sam Brannen @sam_brannen Spring and Java Consultant @ Swiftmind Developing Java for over 14 years Spring Framework Core Committer Spring Trainer Lead author of Spring in a Nutshell Presenter on Spring, Java, and testing
  3. 3. Rossen Stoyanchev @rstoya05 Staff Engineer, VMware Spring Framework Commiter Focus on Web development Spring MVC, Spring Web Flow Teaching and consulting
  4. 4. Themes New testing features in the Spring Framework Spring MVC Test (spring-test-mvc)
  5. 5. Agenda Spring TestContext Framework Web Application Contexts Context Initializers Context Hierarchies Spring MVC Test Framework Server-side Spring MVC Tests Client-side RestTemplate Tests Q&A
  6. 6. Show of Hands... JUnit / TestNG Spring 2.5 / 3.0 / 3.1 / 3.2 Integration testing with Spring Spring TestContext Framework Spring MVC / Test
  7. 7. Spring TestContext Framework
  8. 8. Introduced in Spring 2.5
  9. 9. Revised in Spring 3.2 ...
  10. 10. with a focus on web apps
  11. 11. Unit and integration testing
  12. 12. Annotation-driven
  13. 13. Convention over Configuration
  14. 14. JUnit & TestNG
  15. 15. Spring & Integration Testing ApplicationContext management & caching Dependency injection of test instances Transactional test management with default rollback semantics JdbcTestUtils (-SimpleJdbcTestUtils-)
  16. 16. Spring Test Annotations Application Contexts @ContextConfiguration, @DirtiesContext Dependency Injection @Autowired Transactions @Transactional, @TransactionConfiguration, @BeforeTransaction Web @WebAppConfiguration
  17. 17. Using the TestContext Framework Use the SpringJUnit4ClassRunner for JUnit 4.5+ Instrument test class with TestContextManager for TestNG Extend one of the base classes Abstract(Transactional)[JUnit4|TestNG]SpringContextTests
  18. 18. Example: @POJO Test Class public class OrderServiceTests { @Test public void testOrderService() { … } }
  19. 19. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) public class OrderServiceTests { @Test public void testOrderService() { … } }
  20. 20. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class OrderServiceTests { @Test public void testOrderService() { … } }
  21. 21. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // defaults to // OrderServiceTests-context.xml in same package public class OrderServiceTests { @Test public void testOrderService() { … } }
  22. 22. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // defaults to // OrderServiceTests-context.xml in same package public class OrderServiceTests { @Autowired private OrderService orderService; @Test public void testOrderService() { … } }
  23. 23. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // defaults to // OrderServiceTests-context.xml in same package @Transactional public class OrderServiceTests { @Autowired private OrderService orderService; @Test public void testOrderService() { … } }
  24. 24. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // defaults to // OrderServiceTests-context.xml in same package @Transactional public class OrderServiceTests { @Autowired private OrderService orderService; @BeforeTransaction public void verifyInitialDatabaseState() { … } @Test public void testOrderService() { … } }
  25. 25. Example: @POJO Test Class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // defaults to // OrderServiceTests-context.xml in same package @Transactional public class OrderServiceTests { @Autowired private OrderService orderService; @BeforeTransaction public void verifyInitialDatabaseState() { … } @Before public void setUpTestDataWithinTransaction() { … } @Test public void testOrderService() { … } }
  26. 26. Core Components
  27. 27. TestContext Tracks context for current test Delegates to a ContextLoader Caches the ApplicationContext
  28. 28. TestContextManager Manages the TestContext Signals events to listeners
  29. 29. TestExecutionListener SPI Reacts to test execution events Receives reference to current TestContext
  30. 30. TestExecutionListener 2.5
  31. 31. SmartContextLoader SPI Strategy for loading application contexts From @Configuration classes or resource locations Supports environment profiles and context initializers
  32. 32. ContextLoader 3.1
  33. 33. Putting it all together
  34. 34. What's New in 3.2
  35. 35. Upgraded to JUnit 4.10 and TestNG 6.5.2
  36. 36. spring-test now depends on junit:junit-dep
  37. 37. Now inferring return type of generic factory methods
  38. 38. Updated Spring mocks
  39. 39. Simplified transaction manager configuration
  40. 40. Improved JDBC integration testing support
  41. 41. Documented support for...
  42. 42. @Bean lite mode
  43. 43. and
  44. 44. JSR-250 lifecycle annotations
  45. 45. Now supporting...
  46. 46. WebApplicationContext
  47. 47. session & request scoped beans
  48. 48. and
  49. 49. ApplicationContextInitializer
  50. 50. Planned support for...
  51. 51. context hierarchies
  52. 52. Generic Factory Methods Not specific to testing Sometimes used for creating a proxy But often used for mocking Spring beans
  53. 53. Example: Mocking with EasyMock <beans ...> <!-- OrderService is autowired with OrderRepository --> <context:component-scan base-package="com.example"/> <bean id="orderRepository" class="org.easymock.EasyMock" factory-method="createMock" c:_="com.example.repository.OrderRepository" /> </beans>
  54. 54. EasyMock.createMock() Signature public static <T> T createMock(Class<T> toMock) {...}
  55. 55. Generic Factory Methods - Improved Generic return types are now inferred public static <T> T mock(Class<T> clazz) public static <T> T proxy(T obj) Autowiring by type now works And custom solutions are now chiefly unnecessary: MockitoFactoryBean EasyMockFactoryBean Springockito
  56. 56. Spring Environment Mocks introduced MockEnvironment in 3.2 complements MockPropertySource from 3.1
  57. 57. New Spring HTTP Mocks Client: MockClientHttpRequest MockClientHttpResponse Server: MockHttpInputMessage MockHttpOutputMessage
  58. 58. Enhanced Servlet API Mocks MockServletContext MockHttpSession MockFilterChain MockRequestDispatcher ...
  59. 59. Transaction Manager Config support for single, unqualified transaction manager support for TransactionManagementConfigurer @TransactionConfiguration is now rarely necessary
  60. 60. JDBC Testing Support deprecated SimpleJdbcTestUtils in favor of JdbcTestUtils introduced countRowsInTableWhere() and dropTables() in JdbcTestUtils introduced jdbcTemplate, countRowsInTableWhere(), and dropTables() in AbstractTransactionalJUnit4SpringContextTests AbstractTransactionalTestNGSpringContextTests
  61. 61. Documentation support for @Bean lite mode and annotated classes support for JSR-250 lifecycle annotations
  62. 62. Web Testing Support in the TestContext Framework
  63. 63. New Web Testing Features First-class WebApplicationContext support Request and Session scoped beans
  64. 64. Loading a WebApplicationContext
  65. 65. How do you tell the TestContext framework to load a WebApplicationContext?
  66. 66. Just annotate your test class with ...
  67. 67. @WebAppConfiguration Denotes that the ApplicationContext should be a WebApplicationContext Configures the resource path for the web app used in the MockServletContext Defaults to "src/main/webapp" Paths are file-system folders, relative to the project root not classpath resources The classpath: prefix is also supported
  68. 68. Example: @WebAppConfiguration // defaults to "file:src/main/webapp" @WebAppConfiguration // detects "WacTests-context.xml" in same package // or static nested @Configuration class @ContextConfiguration public class WacTests { //... }
  69. 69. Example: @WebAppConfiguration // file system resource @WebAppConfiguration("webapp") // classpath resource @ContextConfiguration("/spring/test-servlet-config.xml") public class WacTests { //... }
  70. 70. Example: @WebAppConfiguration // classpath resource @WebAppConfiguration("classpath:test-web-resources") // file system resource @ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml") public class WacTests { //... }
  71. 71. Web Context Loaders New AbstractGenericWebContextLoader And two concrete subclasses: XmlWebContextLoader AnnotationConfigWebContextLoader Plus a WebDelegatingSmartContextLoader
  72. 72. SmartContextLoader 3.2
  73. 73. ServletTestExecutionListener Sets up default thread-local state via RequestContextHolder before each test method Creates: MockHttpServletRequest MockHttpServletResponse ServletWebRequest Ensures that the MockHttpServletResponse and ServletWebRequest can be injected into the test instance Cleans up thread-local state after each test method
  74. 74. TestExecutionListener 2.5
  75. 75. TestExecutionListener 3.2
  76. 76. Example: Injecting Mocks @WebAppConfiguration @ContextConfiguration public class WacTests { @Autowired WebApplicationContext wac; // cached @Autowired MockServletContext servletContext; // cached @Autowired MockHttpSession session; @Autowired MockHttpServletRequest request; @Autowired MockHttpServletResponse response; @Autowired ServletWebRequest webRequest; //... }
  77. 77. Web Application Context Caching
  78. 78. WebMergedContextConfiguration Extension of MergedContextConfiguration Supports the base resource path from @WebAppConfiguration which is now part of the context cache key
  79. 79. Request and Session Scoped Beans
  80. 80. Web Scopes request: lifecycle tied to the current HttpServletRequest session: lifecycle tied to the current HttpSession
  81. 81. Example: Request-scoped Bean Config <beans ...> <bean id="userService" class="com.example.SimpleUserService" c:loginAction-ref="loginAction" /> <bean id="loginAction" class="com.example.LoginAction" c:username="#{request.getParameter('user')}" c:password="#{request.getParameter('pswd')}" scope="request"> <aop:scoped-proxy /> </bean> </beans>
  82. 82. Example: Request-scoped Bean Test @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration public class RequestScopedBeanTests { @Autowired UserService userService; @Autowired MockHttpServletRequest request; @Test public void requestScope() { request.setParameter("user", "enigma"); request.setParameter("pswd", "$pr!ng"); LoginResults results = userService.loginUser(); // assert results } }
  83. 83. Example: Session-scoped Bean Config <beans ...> <bean id="userService" class="com.example.SimpleUserService" c:userPreferences-ref="userPreferences" /> <bean id="userPreferences" class="com.example.UserPreferences" c:theme="#{session.getAttribute('theme')}" scope="session"> <aop:scoped-proxy /> </bean> </beans>
  84. 84. Example: Session-scoped Bean Test @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration public class SessionScopedBeanTests { @Autowired UserService userService; @Autowired MockHttpSession session; @Test public void sessionScope() throws Exception { session.setAttribute("theme", "blue"); Results results = userService.processUserPreferences(); // assert results } }
  85. 85. Application Context Initializers
  86. 86. ApplicationContextInitializer Introduced in Spring 3.1 Used for programmatic initialization of a ConfigurableApplicationContext For example: to register property sources to activate profiles against the Environment Configured in web.xml by specifying contextInitializerClasses via context-param for the ContextLoaderListener init-param for the DispatcherServlet
  87. 87. Using Initializers in Tests Configured in @ContextConfiguration via the initializers attribute Inheritance can be controlled via the inheritInitializers attribute An ApplicationContextInitializer may configure the entire context XML resource locations or annotated classes are no longer required Initializers are now part of the context cache key Initializers are ordered based on Spring's Ordered interface or the @Order annotation
  88. 88. Example: Single Initializer @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( locations = "/app-config.xml", initializers = CustomInitializer.class) public class ApplicationContextInitializerTests {}
  89. 89. Example: Multiple Initializers @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( locations = "/app-config.xml", initializers = {PropertySourceInitializer.class, ProfileInitializer.class}) public class ApplicationContextInitializerTests {}
  90. 90. Example: Merged Initializers @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( classes = BaseConfig.class, initializers = BaseInitializer.class) public class BaseTest {} @ContextConfiguration( classes = ExtendedConfig.class, initializers = ExtendedInitializer.class) public class ExtendedTest extends BaseTest {}
  91. 91. Example: Overridden Initializers @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( classes = BaseConfig.class, initializers = BaseInitializer.class) public class BaseTest {} @ContextConfiguration( classes = ExtendedConfig.class, initializers = ExtendedInitializer.class, inheritInitializers = false) public class ExtendedTest extends BaseTest {}
  92. 92. Example: Initializer w/o Locations or Classes @ContextConfiguration(initializers = EntireAppInitializer.class) public class InitializerWithoutConfigFilesOrClassesTest {}
  93. 93. Application Context Hierarchies
  94. 94. WARNING Support for context hierarchies has not yet been implemented.
  95. 95. Status Quo for Tests
  96. 96. Currently only flat, non-hierarchical contexts are supported.
  97. 97. There is no easy way to create contexts with parent-child relationships.
  98. 98. But hierarchies are supported in production.
  99. 99. So it would be nice to be able to test them. ;)
  100. 100. Context Hierarchy Goals Load a test application context with a parent context Support common hierarchies Root WAC <-- Dispatcher WAC EAR <-- Root WAC <-- Dispatcher WAC
  101. 101. Context Hierarchy Proposal Introduce @ContextHierarchy that contains nested @ContextConfiguration declarations Introduce a name attribute in @ContextConfiguration for merging or overriding named configuration in the context hierarchy
  102. 102. Single Test with Context Hierarchy @RunWith(SpringJUnit4ClassRunner.class) @ContextHierarchy({ @ContextConfiguration("parent.xml"), @ContextConfiguration("child.xml") }) public class AppCtxHierarchyTests {}
  103. 103. Root WAC & Dispatcher WAC @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration( name="root", classes = WebAppConfig.class), @ContextConfiguration( name="dispatcher", locations="/spring/dispatcher-config.xml") }) public class ControllerIntegrationTests {}
  104. 104. Class & Context Hierarchies @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml") public abstract class AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml") public class SoapWebServiceTests extends AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/rest-ws-config.xml") public class RestWebServiceTests extends AbstractWebTests {}
  105. 105. Feedback is Welcome SPR-5613: context hierarchy support SPR-9863: web context hierarchy support
  106. 106. Intermission... ;)
  107. 107. Spring MVC Test Framework
  108. 108. Background Recently added to spring-test as of Spring 3.2 RC1 Originates from spring-test-mvc separate project on Github Nearly identical code bases spring-test-mvc will continue to support Spring 3.1
  109. 109. Differences with spring-test-mvc Dependency on Spring 3.2, not Spring 3.1 Support for Spring 3.2 features (e.g. Servlet 3 async) Integration with @WebAppConfiguration Different packages Easy migration from spring-test-mvc to Spring 3.2
  110. 110. What does it provide? 1st class support for testing Spring MVC apps Fluent API Server-side tests involve the DispatcherServlet Client-side tests are RestTemplate-based
  111. 111. Built on spring-test TestContext framework used for loading Spring config MockHttpServletRequest/Response MockFilterChain Servlet container is not used
  112. 112. Extent of support Pretty much everything should work as it does at runtime HTTP message conversion e.g. @RequestBody/@ResponseBody Most rendering technologies JSON, XML, Freemarker/Velocity, Thymeleaf, Excel, etc
  113. 113. Limitations We are not in a servlet container Foward and redirect not executed No JSP rendering
  114. 114. Server-side Spring MVC Tests
  115. 115. Example
  116. 116. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("servlet-context.xml") public class SampleTests { }
  117. 117. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("servlet-context.xml") public class SampleTests { @Autowired private WebApplicationContext wac; private MockMvc mvc; @Before public void setup() { this.mvc = webAppContextSetup(this.wac).build(); } }
  118. 118. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("servlet-context.xml") public class SampleTests { @Autowired private WebApplicationContext wac; private MockMvc mvc; @Before public void setup() { this.mvc = webAppContextSetup(this.wac).build(); } @Test public void getFoo() throws Exception { this.mvc.perform(get("/foo").accept("application/json")) .andExpect(status().isOk()) .andExpect(content().mimeType("application/json")) .andExpect(jsonPath("$.name").value("Lee")); } }
  119. 119. Static Imports MockMvcBuilders.*, MockMvcRequestBuilders.*, MockMvcResultMatchers.* Add as "favorite packages" in Eclipse prefs for code completion Or simply search "MockMvc*"
  120. 120. Set-Up Options The goal: MockMvc instance ready to perform requests Prepare DispatcherServlet config Specify root of webapp Add servlet Filters
  121. 121. Option 1: TestContext framework Load actual DispatcherServlet config Smart caching of WebApplicationContext instances Possibly inject controllers with mock services
  122. 122. Declaring a Mock Service For Injection <bean class="org.mockito.Mockito" factory-method="mock"> <constructor-arg value="org.example.FooService"/> </bean>
  123. 123. Option 2: "Standalone" Simply register one or more @Controller instances Config similar to MVC Java Config No Spring context is actually loaded "Stub" WebApplicationContext used to configure DispatcherServlet
  124. 124. "Standalone" Setup Example standaloneSetup(new PersonController()).build() .mockMvc.perform(post("/persons").param("name", "Joe")) .andExpect(status().isMovedTemporarily()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name"));
  125. 125. @WebAppConfiguration vs. "Standalone" "Standalone" more targetted one controller at a time, explicit config @WebAppConfiguration verifies application config No right or wrong choice, different test styles May also mix and match
  126. 126. Performing Requests Specify HTTP method and URI at a minimum Additional builder-style methods corresponding to MockHttpServletRequest fields
  127. 127. Request Examples mockMvc.perform( post("/hotels/{id}?n={n}", 42, "N")); mockMvc.perform( fileUpload("/doc").file("a1", "ABC".getBytes("UTF-8")));
  128. 128. Specifying Parameters Query string in the URI get("/hotels?a=b") Form params in the request body get("/hotels").body("a=b".getBytes("ISO-8859-1")) Servlet parameters get("/hotels").param("a", "b")
  129. 129. Context/Servlet Path + PathInfo If you specify full path get("/app/main/hotels/{id}") Then set paths accordingly get("").contextPath("/app").servletPath("/main") Or leave out context/servlet path get("/hotels/{id}")
  130. 130. Default Request Properties Performed requests are often similar Same header, parameter, cookie Specify default request when setting up MockMvc Performed requests override defaults!
  131. 131. Default Request Example webAppContextSetup(this.wac).defaultRequest(get("/") .accept(MediaType.APPLICATION_JSON) .param("locale", "en_US"));
  132. 132. Defining Expectations Simply add one or more .andExpect(..) After the call to perform(..) MockMvcResultMatchers provides many choices
  133. 133. What Can Be Verified Response status, headers, content But also Spring MVC specific information Flash attrs, handler, model content, etc See lots of sample tests for examples
  134. 134. JSON and XML Content JSONPath XPath XMLUnit
  135. 135. In Doubt? Print all details mvc.perform("/foo").andDo(print()) Or directly acccess the result MvcResult r = mvc.perform("/foo").andReturn()
  136. 136. Common Expectations Tests often have similar expectations Same status, headers Specify such expectations when setting up MockMvc
  137. 137. Common Expectations Example webAppContextSetup(this.wac) .andAlwaysExpect(status.isOk()) .alwaysExpect(content().mimeType("application/xml")) .alwaysDo(print()) .build();
  138. 138. Filters Filters may be added when setting up MockMvc Executed as requests get peformed Various possible use case See Spring Security sample tests
  139. 139. HtmlUnit Integration Adapts HtmlUnit request-response to MockHttpServletRequest/Response Enables use of Selenium and Geb Spock on top No running server Give it a try, feedback welcome!
  140. 140. Client-side RestTemplate Tests
  141. 141. Example RestTemplate restTemplate = new RestTemplate(); MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); mockServer.expect(requestTo("/greeting")) .andRespond(withSuccess("Hello world", "text/plain")); // use RestTemplate ... mockServer.verify();
  142. 142. Static Imports MockRestRequestMatchers.* and MockRestResponseCreators.* Add as "favorite packages" in Eclipse prefs for code completion Or simply search "MockRest*"
  143. 143. RestTemplate Instrumentation MockRestServiceServer.createServer(restTemplate) Configures RestTemplate with custom ClientHttpRequestFactory Can be used further to ... Define expected requests and responses Verify expectations
  144. 144. Define Expected Requests-Responses mockServer.expect(..).andExpect(..) Followed by .andReturn(..) Set up any number of requests with mockServer.expect(..)
  145. 145. Each Time RestTemplate Is Used Request expectations asserted Stub response returned Exception raised if no more expected requests
  146. 146. After RestTemplate Is Used mockServer.verify(..) Ensures all expected requests executed
  147. 147. In Closing ...
  148. 148. Presentation Source https://github.com/rstoyanchev/spring-32-test-webapps
  149. 149. Resources for Spring MVC Test Spring MVC Showcase tests Sample client and server tests in the Spring Framework Reference documentation will be added in 3.2 RC2
  150. 150. Resources for Core Spring Spring Framework: www.springsource.org/spring-framework Reference Manual: Spring 3.2.x Forums: forum.springframework.org JIRA: jira.springsource.org
  151. 151. Q&A Sam Brannen Web: Swiftmind.com Twitter: @sam_brannen Rossen Stoyanchev Web: SpringSource.com Twitter: @rstoya05

×