/springboot-testing-mysql

Goals: 1) Create a simple Spring Boot application to manage users; 2) Explore the utilities and annotations that Spring Boot provides for testing; 3) Test using Testcontainers.

Primary LanguageJava

springboot-testing-mysql

The goals of this project are:

  • Create a simple Spring Boot application to manage users called user-service. The database used is MySQL;
  • Explore the utilities and annotations that Spring Boot provides for testing applications;
  • Test using Testcontainers.

Proof-of-Concepts & Articles

On ivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.

Project Diagram

project-diagram

Application

  • user-service

    Spring Boot Web Java application to manage users. The data is stored in MySQL.

    user-service-swagger

Prerequisites

Start Environment

  • Open a terminal and inside springboot-testing-mysql root folder run:

    docker compose up -d
    
  • Wait for MySQL Docker container to be up and running. To check it, run:

    docker compose ps
    

Start Application

  • In a terminal, make sure you are in springboot-testing-mysql root folder;

  • Run application:

    ./gradlew user-service:clean user-service:bootRun
    
  • Swagger website is http://localhost:8080/swagger-ui.html

Shutdown

  • Go to the terminal where user-service is running and press Ctrl+C;

  • In a terminal and inside springboot-testing-mysql root folder, run to command below to stop and remove docker compose mysql container and network:

    docker compose down -v
    

Running Unit and Integration Tests

  • In a terminal, navigate to springboot-testing-mysql root folder;

  • Running Tests

    • Unit Tests only:

      ./gradlew user-service:clean user-service:cleanTest user-service:test
      
    • Unit and Integration Tests:

      ./gradlew user-service:clean user-service:cleanTest user-service:check user-service:integrationTest
      

      Note: During the tests, Testcontainers starts automatically MySQL Docker container before the tests begin and shuts it down when the tests finish.

  • Unit Test Report can be found at:

    user-service/build/reports/tests/test/index.html
    
  • Integration Test Report can be found at:

    user-service/build/reports/tests/integrationTest/index.html
    

Useful Commands

  • MySQL
    docker exec -it -e MYSQL_PWD=secret mysql mysql -uroot --database userdb
    SELECT * FROM users;
    

References