Code Monkey home page Code Monkey logo

spring-boot-testing-strategies's Introduction

Spring Boot Testing Strategies

Introduction

This sample application made with Spring Boot is intended to show the different approach for testing, from Unit Tests with MockMVC in Standalone mode to full @SpringBootTest as Integration tests between the modules.

The complete guide is available on The Practical Developer Blog.

The application

The logic behind the application is simple: it's a repository of superheroes that you can access through a REST API. It allows to read the available ones (which are hardcoded when the application starts up) and also add new members to the crew.

The architecture is simple: just the Controller layer (REST) and a SuperHeroRepository. To illustrate the differences when creating tests, there are two extra classes that work at a web layer level:

  • SuperHeroExceptionHandler. It's a ControllerAdvice that will transform a NonExistingHeroException into a 404 NOT_FOUND HTTP error code.
  • SuperHeroFilter. This web filter adds a new header to the HTTP response.

Testing strategies

In the test sources you can find four different approaches to test the Controller. SuperHeroControllerMockMvcStandaloneTest. Uses a MockitoJUnitRunner and it's the most lightweight approach.

MockMVC in Standalone mode

Then you can find two approaches using a Spring context, both use MockMVC and one of them already introduces the @SpringBootTest annotation.

MockMVC using the context

Finally, SuperHeroControllerSpringBootTest shows how to write a @SpringBootTest based test mocking other layers but utilizing the web server with a RestTemplate.

@SpringBootTest using context and web server

To check conclusion and more information please visit the blog.

spring-boot-testing-strategies's People

Contributors

mechero 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.