Git Product home page Git Product logo

spring-testing-1's Introduction

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.

spring-testing-1's People

Contributors

matsev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.