spring-testing icon indicating copy to clipboard operation
spring-testing copied to clipboard

Small demo project intended to show how some of the Spring features can be used to improve your Spring test suite.

Spring Testing

This is a simplistic demo project intended to show how some Spring features can be used to improve your Spring test suite. The project has been used as demo during conference sessions. During SpringOne 2GX 2013 the session was recorded and you can watch it on YouTube. Below you will find references to where the concepts in the presentation have been used in the source code.

Embedded database

An example of an application context with an embedded database can be found in EmbeddedDbJavaConfig class.

Test of the example database can be seen in EmbeddedDbJavaConfigTest.

Transactional Tests

Transactions are used in some tests, e.g. AccountEntityTransactionalTest.

Remember to flush() your JPA entity managers and your Hibernate sessions to avoid false positives when testing.

Spring Profiles

The RepositoryConfig interface has been implemented using three different profiles:

  • h2 uses a H2 in-memory database, see H2RepositoryConfig.
  • mysql connects to a local MySQL database, see MySqlRepositoryConfig.
  • prod simulates a JNDI database resource lookup, see JndiRepositoryConfig.

All three repository config classes above have been imported by the ApplicationConfig class, but only one can be used at the time, by settings the spring.profiles.active environment variable. The prod profile has been set to the default profile by using the spring.profiles.default context parameter in web.xml.

In some tests, like the AccountServiceImplTest, the @ActiveProfiles annotation has been used to specify which profile should be active in the test.

Mockito

A pure Mockito test that does not use any Spring related tools is exemplified in BankControllerBasicTest.

If a mock object is used as a Spring bean in an application context, it should be reset() @Before or @After a test to certify that it is always in clean state before the next test is executed.

Controller Tests

The test BankControllerMvcTest uses the MockMvc class to verify request mapping, serialization, response codes, etc.

Integration Tests

The BankApplicationTest is a Spring based integration tests that tests the entire stack based on pure Spring features, MockMvc, @Transactional, embedded database, etc.

The maven-failsafe-plugin has been added to the [pom.xml] file to automate the integration tests. It executes the integration test during the integration-test phase (in contrast to the maven-surefire-plugin that executes tests in the test phase). Moreover, it is activated by using the Maven itest profile.

The RestTemplateBankApplicationIT is an integration test based on Spring's RestTemplate. A similar test based on REST-assured can be found in RestAssuredBankApplicationIT In order to execute them, you must have a MySQL running locally that Spring can connect to. Alternatively, you can change the profile from mysql to h2 the tests will use and embedded H2 database instead.