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 with Spring Framework 4.x

13,256 views

Published on

Speaker: Sam Brannen
Core Spring Track
The Spring Framework has undergone a lot of innovation in the 4.0 and 4.1 releases, and so has its testing support. Join Spring Test component lead Sam Brannen in this talk to discover what's new in Spring's testing support in 4.0 through 4.1.

This talk will provide attendees an overview of what's been deprecated, what's changed, and what's been introduced in Spring's testing support over the last two years, with real life examples and tips for best practices. Highlights include using SocketUtils to scan for free TCP & UDP server ports, the ActiveProfilesResolver API, meta-annotation support for test annotations including attribute overrides, best practices with TestNG, using Groovy scripts to configure an ApplicationContext for integration tests, improvements to SQL script execution and embedded databases, the new TestContext framework bootstrap strategy, programmatic transaction management in tests, and more.

Published in: Software

Testing with Spring Framework 4.x

  1. 1. Testing with Spring 4.x Sam Brannen @sam_brannen U©n 2le0s1s2 -o2t0h1e4rw Sisaem i nBdriacnanteedn, atnhde sleic eslnidseeds aurned e©r 2a0 C12re-2a0ti1ve4 CSaomm mBorannsn Aetntr iabnudti olince-NnosendC oumndmeerr aci al license Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
  2. 2. Sam Brannen • Spring and Java Consultant @ Swiftmind • Java Developer for over 15 years • Spring Framework Core Committer since 2007 • Component lead for spring-test • Spring Trainer • Speaker on Spring, Java, and testing • Swiss Spring User Group Lead © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 2
  3. 3. Areas of expertise – Spring * – Java EE – Software Architecture – Software Engineering Best Practices Where you find us • Zurich, Switzerland • @swiftmind • http://www.swiftmind.com © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 3 Your experts for Spring and Enterprise Java
  4. 4. A show of hands… © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 4 ? ? ? ? ?
  5. 5. Agenda • Deprecations, Pruning, & Dependencies • Recap of Testing with Spring 3.x • Testing Themes in 4.x • Details, Tips, & Examples • Q&A © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 5
  6. 6. Deprecations, Pruning, & Dependencies © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 6
  7. 7. Spring Cleaning in 4.x • All deprecated packages removed • Many deprecated methods and fields removed as well • Mind the deprecation warnings… before upgrading from 3.x © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 7
  8. 8. Pruning in 4.0 • JUnit 3.8 support • Use JUnit 4 or TestNG • @ExpectedException • Use @Test(expected) or @Rule ExpectedException in JUnit • Use @Test(expectedExceptions) in TestNG • @NotTransactional • Use @Transactional(propagation=NOT_SUPPORTED) • SimpleJdbcTestUtils • Use JdbcTestUtils, ScriptUtils, or @Sql © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 8
  9. 9. Dependency Upgrades in 4.0 • Servlet API mocks • Now based on Servlet 3.0 • Servlet 2.5 still supported in production • JUnit • Tested against à 4.11 • TestNG • Tested against à 6.8.5 © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 9
  10. 10. Dependency Upgrades in 4.1 • JUnit • Minimum version à 4.9 • Recommended à 4.11 • TestNG • Tested against à 6.8.8 © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 10
  11. 11. Testing with Spring 3.x © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 11
  12. 12. Testing Themes from Spring 3.x (1/2) • Embedded databases • <jdbc:embedded-database /> & <jdbc:initialize-database /> • EmbeddedDatabaseBuilder & EmbeddedDatabaseFactoryBean • @Configuration classes • @ActiveProfiles • ApplicationContextInitializers © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 12
  13. 13. Testing Themes from Spring 3.x (2/2) • @WebAppConfiguration • Loading WebApplicationContexts • Testing request- and session-scoped beans • @ContextHierarchy • Web, Batch, etc. • Spring MVC Test framework • Server-side MVC and REST tests • Client-side REST tests © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 13
  14. 14. Ex: Web Integration Test (1/2) © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 14 @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = RootConfig.class), @ContextConfiguration(classes = WebConfig.class) }) @ActiveProfiles("dev") public class ControllerIntegrationTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; // ...
  15. 15. Ex: Web Integration Test (2/2) © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 15 @Before public void setup() { this.mockMvc = MockMvcBuilders .webAppContextSetup(this.wac).build(); } @Test public void person() throws Exception { this.mockMvc.perform(get("/person/42") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string("{"name":"Sam"}")); }
  16. 16. Testing Themes in Spring 4.0 © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 16
  17. 17. New in 4.0 (1/3) • SocketUtils • Scan for available UDP & TCP ports • ActiveProfilesResolver API • Programmatic alternative to static profile strings • Set via new resolver attribute in @ActiveProfiles • Meta-annotation support for tests • Attribute overrides (optional and required) © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 17
  18. 18. New in 4.0 (2/3) • New deleteFromTableWhere() method in AbstractTransactional*SpringContextTests • * à JUnit4 or TestNG • New verify() and reset() methods in AnnotationDrivenStaticEntityMockingControl • Multi-line SQL comments: • ResourceDatabasePopulator, JdbcTestUtils, ScriptUtils © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 18
  19. 19. New in 4.0 (3/3) • TestContext converted to an interface • Allows TestContext to be mocked in unit tests • Simultaneous use of classes and locations in @ContextConfiguration for hybrid loaders • See Spring Boot • Servlet API mock improvements © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 19
  20. 20. Testing Themes in Spring 4.1 © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 20
  21. 21. New in 4.1 – Context Config • Context config with Groovy scripts • Declarative configuration for test property sources • @TestPropertySource © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 21
  22. 22. New in 4.1 – Transactions and SQL • Programmatic test transaction management • TestTransaction API • Declarative SQL script execution • @Sql, @SqlConfig, @SqlGroup • Improved docs for transactional tests © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 22
  23. 23. New in 4.1 – Bootstrap & TestExecutionListeners • TestContext bootstrap strategy • TestContextBootstrapper & @BootstrapWith • Automatic discovery of default TestExecutionListeners • Uses SpringFactoriesLoader • Already used by Spring Security • Merging custom TestExecutionListeners with defaults • @TestExecutionListeners(mergeMode=MERGE_WITH_DEFAULTS) • Defaults to REPLACE_DEFAULTS © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 23
  24. 24. New in 4.1 – Spring MVC Test • Assert JSON responses with JSON Assert • Complements JSONPath support • Create MockMvcBuilder recipes with MockMvcConfigurer • Developed to apply Spring Security setup but can be used by anyone • AsyncRestTemplate support in MockRestServiceServer • For asynchronous client-side testing © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 24
  25. 25. New in 4.1 – Odds & Ends • AssertThrows: refactored to support Throwable • Various improvements to Servlet API mocks © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 25
  26. 26. Details, Tips, & Examples © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 26
  27. 27. SocketUtils • Utility class introduced in Spring Framework 4.0 • Located in spring-core • Can be used in production code • But ideal for embedded testing scenarios (SMTP, FTP, etc.) • Finds available TCP and UDP ports on localhost • Default port range: 1024 à 65535 • See Javadoc for all options • Straightforward usage in Java and @Configuration classes © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 27
  28. 28. Using SocketUtils in XML Config • Via fully qualified class name and SpEL <bean id="bean1" ... p:port= "#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(12000)}" /> • Via socketUtils bean and SpEL (as of Spring 4.0.8 & 4.1.1) <bean id="socketUtils" class="org.springframework.util.SocketUtils" /> <bean id="bean1" ... p:port="#{socketUtils.findAvailableTcpPort(12000)}" /> <bean id="bean2" ... p:port="#{socketUtils.findAvailableTcpPort(30000)}" /> © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 28
  29. 29. Tip: Define Free Port as Bean • Define the free port as a Spring Bean… <bean id="serverPort" class="java.lang.Integer" c:_="#{T(socketUtils.findAvailableTcpPort()}" /> • Then reference the serverPort from other beans • Or inject it into components, @Configuration classes, and tests © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 29
  30. 30. Ex: @ActiveProfiles – Declarative © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 30 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @ActiveProfiles("dev") public class IntegrationTests { // ... } But what if static isn’t good enough?
  31. 31. Ex: @ActiveProfiles – Programmatic © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 31 public class MyResolver implements ActiveProfilesResolver { public String[] resolve(Class<?> testClass) { // resolve bean definition profiles for test class } } @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @ActiveProfiles(resolver = MyProfileResolver.class) public class IntegrationTests { // ... } Implement custom resolver And declare it
  32. 32. Composable Stereotypes – Quick Review • Combining meta-annotations on a custom stereotype • Automatically detected: no configuration necessary! © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 32
  33. 33. Duplicate Test Configuration == Bad @ContextConfiguration("/test-config.xml") @ActiveProfiles("dev") @Transactional public class OrderRepositoryTests { } @ContextConfiguration("/test-config.xml") @ActiveProfiles("dev") @Transactional public class UserRepositoryTests { } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 33 duplication duplication
  34. 34. Custom Test Annotation == Good @Target(TYPE) @Retention(RUNTIME) @ContextConfiguration("/test-config.xml") @ActiveProfiles("dev") @Transactional public @interface TransactionalDevTest { } @TransactionalDevTest public class OrderRepositoryTests { } @TransactionalDevTest public class UserRepositoryTests { } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 34 common config
  35. 35. Composable Annotations with Overrides • Composable annotations may override attributes of meta-annotations • Purely convention-based • Matched by attribute name and type • Can lead to potential naming conflicts • Cannot override the value attribute © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 35
  36. 36. Optional Annotation Attribute Override @Target(TYPE) @Retention(RUNTIME) @ContextConfiguration @Transactional public @interface TransactionalTest { default locations declared here will be ignored String[] locations() default "/test-config.xml"; } @TransactionalTest(locations = "/order-test-config.xml") public class OrderRepositoryTests { } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 36 optional: overrides default
  37. 37. Required Annotation Attribute Override @Target(TYPE) @Retention(RUNTIME) @ContextConfiguration @Transactional public @interface TransactionalTest { String[] locations(); } @TransactionalTest(locations = "/order-test-config.xml") public class OrderRepositoryTests { } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 37 required no default locations declared here will be ignored
  38. 38. Groovy Beans in Spring • Spring Framework 4.0 introduced support for the Groovy Bean Definition DSL via the GroovyBeanDefinitionReader and GenericGroovyApplicationContext • Spring Framework 4.1 introduces support for Groovy scripts in web applications via the GroovyWebApplicationContext • Testing support added in 4.1… © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 38
  39. 39. Spring Bean Definitions with Groovy DSL import org.mypackage.domain.Person; beans { xmlns util: 'http://www.springframework.org/schema/util' person1(Person) { name = "homer" age = 45 props = [overweight: true, height: "1.8m"] children = ["bart", "lisa"] } util.list(id: 'foo') { value 'one' value 'two' } } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 39
  40. 40. Groovy Scripts for Context Config in Tests • Spring Framework 4.1 introduces support for Groovy scripts in integration tests via @ContextConfiguration • Scripts are configured via the locations or value attribute o Resource semantics identical to XML o Default detected with “Context.groovy” suffix in same package • The inheritLocations flag is fully supported • Groovy and XML configuration can be declared together • Groovy WebApplicationContexts supported via @WebAppConfiguration © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 40
  41. 41. Ex: Groovy Script Config @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/context.groovy") public class GroovyPersonTests { @Autowired private Person person; /* test methods using person bean */ } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 41
  42. 42. Ex: Default Groovy Script Detection public com.example; @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from // “classpath:com/example/MyTestContext.groovy” @ContextConfiguration public class MyTest { /* ... */ } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 42
  43. 43. Ex: Groovy & XML Config Together @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from // “/context.groovy” and “/context.xml” @ContextConfiguration({ "/context.groovy", "/context.xml" }) public class MyTest { /* ... */ } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 43
  44. 44. Test Property Sources • Spring 3.1 introduced PropertySources abstraction • Configured via Environment or via @PropertySource • Spring 4.1 supports declarative test property sources • Configured via @TestPropertySource • Test property sources are declared via annotation attributes • locations or value: resource locations • properties: inlined properties • both are inherited by default © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 44
  45. 45. @TestPropertySource – locations • String array of resource locations for Java Properties files • Both traditional *.properties and XML formats are supported • Resource semantics are identical to those for locations in @ContextConfiguration © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 45
  46. 46. Ex: @TestPropertySource – locations @ContextConfiguration @TestPropertySource("/test.properties") public class MyIntegrationTests { // class body... } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 46
  47. 47. @TestPropertySource – properties • Inlined properties can be declared as key/value pairs • Uses syntax for entries in Java properties files: • "key=value" • "key:value" • "key value" © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 47
  48. 48. Ex: @TestPropertySource – properties @ContextConfiguration @TestPropertySource( properties = {"foo=bar", "port: 4242"} ) public class MyIntegrationTests { // class body... } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 48
  49. 49. Default Properties File Detection • If neither locations nor properties are defined, a default properties file will be detected • Default is detected with “.properties” suffix in same package • If the class is com.example.MyTest, the default properties file is “classpath:com/example/MyTest.properties” • Exception is thrown if default is not present © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 49
  50. 50. @TestPropertySource – Precedence © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 50 Inlined Files Application & System test precedence
  51. 51. Ex: @TestPropertySource – locations & properties @ContextConfiguration @TestPropertySource( locations = "/test.properties", properties = "port: 4242" ) public class MyIntegrationTests { // class body... } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 51
  52. 52. Programmatic Transaction Management in Tests • History Lesson: Spring’s JUnit 3.8 testing framework supported endTransaction() and startNewTransaction() methods in AbstractTransactionalSpringContextTests • But… the Spring TestContext Framework, introduced in Spring 2.5, did not… until now • Due to popular demand, Spring 4.1 introduces a new TestTransaction API © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 52
  53. 53. Transactions in Spring • Spring-managed transactions: managed by Spring in the ApplicationContext • @Transactional and AOP • Application-managed transactions: managed programmatically within application code • TransactionTemplate and TransactionSynchronizationManager • Test-managed transactions: managed by the Spring TestContext Framework • @Transactional on test classes and test methods • Transaction is rolled back by default! © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 53
  54. 54. Ex: Declarative Transaction Management in Tests @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @Transactional public class TransactionalTests { @Test public void withinTransaction() { /* ... */ } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 54 What if we want to stop & start the transaction within the test method?
  55. 55. TestTransaction API • Static methods for interacting with test-managed transactions • isActive() • isFlaggedForRollback() • flagForCommit() • flagForRollback() • end() • start() © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 55 query status change default rollback setting end: roll back or commit based on flag start: new tx with default rollback setting
  56. 56. Ex: Programmatic Transaction Management in Tests @Test public void withinTransaction() { // assert initial state in test database: assertNumUsers(2); deleteFromTables("user"); // changes to the database will be committed TestTransaction.flagForCommit(); TestTransaction.end(); assertFalse(TestTransaction.isActive()); assertNumUsers(0); TestTransaction.start(); // perform other actions against the database that will // be automatically rolled back after the test completes... } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 56
  57. 57. Executing SQL Scripts © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 57
  58. 58. Ex: Embedded Database in Java Config © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 58 @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(H2) .setScriptEncoding("UTF-8") .ignoreFailedDrops(true) .addScript("schema.sql") .addScripts("user_data.sql", "country_data.sql") .build(); } API greatly improved in Spring 4.0.3
  59. 59. Ex: Embedded Database in XML Config <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:/schema.sql" /> <jdbc:script location="classpath:/user_data.sql" /> </jdbc:embedded-database> © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 59
  60. 60. Ex: Populate Database in XML Config <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:/schema_01.sql" /> <jdbc:script location="classpath:/schema_02.sql" /> <jdbc:script location="classpath:/data_01.sql" /> <jdbc:script location="classpath:/data_02.sql" /> </jdbc:initialize-database> © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 60
  61. 61. Executing SQL per Test Method • The previous techniques are very useful for setting up the initial database state • Q: But how can we execute SQL scripts per test method? • A: Programmatically via ScriptUtils, ResourceDatabasePopulator, or abstract transactional base test classes for JUnit and TestNG. • Q: OK, but how can we do that declaratively? • A: Via @Sql in Spring Framework 4.1! © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 61
  62. 62. Executing SQL Scripts Declaratively with @Sql • @Sql: declared on a test class or test method • method-level overrides class-level • The scripts attribute is used to declare resource locations for SQL scripts • semantics analogous to locations in @ContextConfiguration • Scripts can be executed before or after a test method • configured via the executionPhase attribute of @Sql © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 62
  63. 63. Ex: @Sql in Action @ContextConfiguration @Sql({ "schema1.sql", "data1.sql" }) public class SqlScriptsTests { @Test public void classLevelScripts() { /* ... */ } @Test @Sql({ "schema2.sql", "data2.sql" }) public void methodLevelScripts() { /* ... */ } © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 63
  64. 64. Default SQL Script Detection • If no scripts are declared, a default script will be detected • Depending on where @Sql is declared • Class-level: for com.example.DbTest, the default is “classpath:com/ example/DbTest.sql” • Method-level: for com.example.DbTest.test(), the default is “classpath:com/example/DbTest.test.sql” • If the default is not present, an exception is thrown © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 64
  65. 65. Declaring Multiple @Sql Sets • Declare multiple sets of @Sql scripts for varying configuration • Java 8: use @Sql as a repeatable annotation • Java 6 & 7: wrap @Sql sets in @SqlGroup © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 65
  66. 66. @Sql as a Repeatable Annotation (Java 8) © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 66 @Test @Sql( scripts="/test-schema.sql", config = @SqlConfig(commentPrefix = "`") @Sql("/user-data.sql") public void userTest() { // code that uses the test schema and test data } Schema uses custom syntax
  67. 67. @Sql wrapped in @SqlGroup (Java 6 & 7) © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 67 @Test @SqlGroup({ @Sql( scripts="/test-schema.sql", config = @SqlConfig(commentPrefix = "`"), @Sql("/user-data.sql") }) public void userTest() { // code that uses the test schema and test data }
  68. 68. Configuring SQL Scripts with @SqlConfig • @SqlConfig: configures script parsing and error handling • Class-level: serves as global configuration for the test class • @Sql(config): serves as local configuration for the enclosing @Sql • Local configuration inherits global configuration and can selectively override global configuration • Transaction management for script execution is configured via the dataSource, transactionManager, and transactionMode attributes • See Javadoc and reference manual for details © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 68
  69. 69. In closing… © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 69
  70. 70. Related Sessions @ SpringOne 2GX 2014 © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 70 • The Quest for the Holy Integration Test • Ken Krueger and Rob Winch • September 10, 2014 • 4:30 PM - 6:00 PM • Building highly modular and testable business systems with Spring Integration • Marius Bogoevici • September 11, 2014 • 10:30 AM - 12:00 PM
  71. 71. Spring Resources Spring Framework: http://projects.spring.io/spring-framework Spring Guides: http://spring.io/guides Spring JIRA: https://jira.spring.io Spring on GitHub: https://github.com/spring-projects/spring-framework Stack Overflow: spring, spring-test, spring-mvc, … © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 71
  72. 72. Blogs Spring Blog: http://spring.io/blog Swiftmind Blog: http://www.swiftmind.com/blog © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 72
  73. 73. Q & A Sam Brannen @sam_brannen www.slideshare.net/sbrannen www.swiftmind.com © 2012-2014 Sam Brannen and licensed under a Creative Commons Attribution-NonCommercial license 73 @springcentral | spring.io/video

×