All Projects → hamvocke → Spring Testing

hamvocke / Spring Testing

A Spring Boot application with lots of sample tests

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Spring Testing

Spring Cloud Zookeeper
Spring Cloud Zookeeper
Stars: ✭ 481 (-15.47%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Security
Security concerns for distributed applications implemented in Spring
Stars: ✭ 488 (-14.24%)
Mutual labels:  microservices, spring-boot, spring
Quickperf
QuickPerf is a testing library for Java to quickly evaluate and improve some performance-related properties
Stars: ✭ 231 (-59.4%)
Mutual labels:  microservices, spring-boot, spring
Gemini
Model Driven REST framework to automatically generate CRUD APIs
Stars: ✭ 138 (-75.75%)
Mutual labels:  microservices, spring-boot, spring
Learn
How do you achieve your career objectives? Complete career paths with amazing Cloud, Full Stack and Microservice Courses and Videos from in28Minutes
Stars: ✭ 447 (-21.44%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Cli
Spring Cloud CLI features
Stars: ✭ 139 (-75.57%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Gateway Sample
Sample Spring Cloud Gateway Application
Stars: ✭ 268 (-52.9%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Build
Common build concerns, shared plugin configuration, etc. for Spring Cloud modules
Stars: ✭ 114 (-79.96%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Bus
Spring Cloud event bus
Stars: ✭ 342 (-39.89%)
Mutual labels:  microservices, spring-boot, spring
Java Spring Cloud
Distributed tracing for Spring Boot, Cloud and other Spring projects
Stars: ✭ 326 (-42.71%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Release
Spring Cloud Release Train - dependency management across a wide range of Spring Cloud projects.
Stars: ✭ 543 (-4.57%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Commons
Common classes used in different Spring Cloud implementations
Stars: ✭ 493 (-13.36%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Config
External configuration (server and client) for Spring Cloud
Stars: ✭ 1,740 (+205.8%)
Mutual labels:  microservices, spring-boot, spring
Reactive Ms Example
An educational project to learn reactive programming with Spring 5
Stars: ✭ 157 (-72.41%)
Mutual labels:  microservices, spring-boot, spring
Java Interview
At the beginning, it was the repository with questions from Java interviews. Currently, it's more like knowledge base with useful links.
Stars: ✭ 114 (-79.96%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Kubernetes
Kubernetes integration with Spring Cloud Discovery Client, Configuration, etc...
Stars: ✭ 2,894 (+408.61%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Sleuth
Distributed tracing for spring cloud
Stars: ✭ 1,531 (+169.07%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Example
Stars: ✭ 111 (-80.49%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Gateway
A Gateway built on Spring Framework 5.x and Spring Boot 2.x providing routing and more.
Stars: ✭ 3,305 (+480.84%)
Mutual labels:  microservices, spring-boot, spring
Spring Cloud Netflix
Integration with Netflix OSS components
Stars: ✭ 4,498 (+690.51%)
Mutual labels:  microservices, spring-boot, spring

Testing Microservices in Spring

Build Status

This repository contains a Spring Boot application with lots of exemplary tests on different levels of the Test Pyramid. It shows an opinionated way to thoroughly test your spring application by demonstrating different types and levels of testing. You will find that some of the tests are duplicated along the test pyramid -- concepts that have already been tested in lower-level tests will be tested in more high-level tests. This contradicts the premise of the test pyramid. In this case it helps showcasing different kinds of tests which is the main goal of this repository.

Read the Blog Post

This repository is part of a series of blog posts I wrote about testing microservices. I highly recommend you read them to get a better feeling for what it takes to test microservices and how you can implement a reliable test suite for a Spring Boot microservice application.

Get started

1. Set an API Key as Environment Variable

In order to run the service, you need to set the WEATHER_API_KEY environment variable to a valid API key retrieved from darksky.net openweathermap.org.

Note: in a previous version this example used darksky.net as the weather API. Since they've shut down their API for public access, we've since switched over to openweathermap.org

A simple way is to rename the env.sample file to .env, fill in your API key from openweathermap.org and source it before running your application:

source .env

2. Start a PostgreSQL database

The easiest way is to use the provided startDatabase.sh script. This script starts a Docker container which contains a database with the following configuration:

  • port: 15432
  • username: testuser
  • password: password
  • database name: postgres

If you don't want to use the script make sure to have a database with the same configuration or modify your application.properties.

3. Run the Application

Once you've provided the API key and started a PostgreSQL database you can run the application using

./gradlew bootRun

The application will start on port 8080 so you can send a sample request to http://localhost:8080/hello to see if you're up and running.

Application Architecture

 ╭┄┄┄┄┄┄┄╮      ┌──────────┐      ┌──────────┐
 ┆   ☁   ┆  ←→  │    ☕     │  ←→  │    💾     │
 ┆  Web  ┆ HTTP │  Spring  │      │ Database │
 ╰┄┄┄┄┄┄┄╯      │  Service │      └──────────┘
                └──────────┘
                     ↑ JSON/HTTP
                     ↓
                ┌──────────┐
                │    ☁     │
                │ Weather  │
                │   API    │
                └──────────┘

The sample application is almost as easy as it gets. It stores Persons in an in-memory database (using Spring Data) and provides a REST interface with three endpoints:

  • GET /hello: Returns "Hello World!". Always.
  • GET /hello/{lastname}: Looks up the person with lastname as its last name and returns "Hello {Firstname} {Lastname}" if that person is found.
  • GET /weather: Calls a downstream weather API via HTTP and returns a summary for the current weather conditions in Hamburg, Germany

Internal Architecture

The Spring Service itself has a pretty common internal architecture:

  • Controller classes provide REST endpoints and deal with HTTP requests and responses
  • Repository classes interface with the database and take care of writing and reading data to/from persistent storage
  • Client classes talk to other APIs, in our case it fetches JSON via HTTP from the openweathermap.org weather API
Request  ┌────────── Spring Service ───────────┐
 ─────────→ ┌─────────────┐    ┌─────────────┐ │   ┌─────────────┐
 ←───────── │  Controller │ ←→ │  Repository │←──→ │  Database   │
Response │  └─────────────┘    └─────────────┘ │   └─────────────┘
         │         ↓                           │
         │    ┌──────────┐                     │
         │    │  Client  │                     │
         │    └──────────┘                     │
         └─────────│───────────────────────────┘
                   │
                   ↓   
              ┌──────────┐
              │    ☁     │
              │ Weather  │
              │   API    │
              └──────────┘

Test Layers

The example applicationn shows different test layers according to the Test Pyramid.

      ╱╲
  End-to-End
    ╱────╲
   ╱ Inte-╲
  ╱ gration╲
 ╱──────────╲
╱   Unit     ╲
──────────────

The base of the pyramid is made up of unit tests. They should make the biggest part of your automated test suite.

The next layer, integration tests, test all places where your application serializes or deserializes data. Your service's REST API, Repositories or calling third-party services are good examples. This codebase contains example for all of these tests.

 ╭┄┄┄┄┄┄┄╮      ┌──────────┐      ┌──────────┐
 ┆   ☁   ┆  ←→  │    ☕     │  ←→  │    💾     │
 ┆  Web  ┆      │  Spring  │      │ Database │
 ╰┄┄┄┄┄┄┄╯      │  Service │      └──────────┘
                └──────────┘

  │    Controller     │      Repository      │
  └─── Integration ───┴──── Integration ─────┘

  │                                          │
  └────────────── Acceptance ────────────────┘               
 ┌─────────┐  ─┐
 │    ☁    │   │
 │ Weather │   │
 │   API   │   │
 │  Stub   │   │
 └─────────┘   │ Client
      ↑        │ Integration
      ↓        │ Test
 ┌──────────┐  │
 │    ☕     │  │
 │  Spring  │  │
 │  Service │  │
 └──────────┘ ─┘

Tools

You can find lots of different tools, frameworks and libraries being used in the different examples:

  • Spring Boot: application framework
  • JUnit: test runner
  • Hamcrest Matchers: assertions
  • Mockito: test doubles (mocks, stubs)
  • MockMVC: testing Spring MVC controllers
  • RestAssured: testing the service end to end via HTTP
  • Wiremock: provide HTTP stubs for downstream services
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].