Skip to content

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

Notifications You must be signed in to change notification settings

matsev/spring-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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:

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.

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published