E2E testing plays an important role in the application development cycle by verifying the end-to-end behavior of a system. This project template uses JUnit 5, Testcontainers and Selenium 4 to test a Spring MVC application with a PostgreSQL database.
This project demonstrates how to test any Spring MVC application with Selenium
and running a database server using Testcontainers
.
Model–view–controller (MVC) is a software design pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements:
- The Model encapsulates the application data and in general they will consist of POJO.
- The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
- The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.
Spring implements MVC with the front controller pattern using its DispatcherServlet. Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.
In local development, you need to provide a connection to a PostgreSQL
instance.
You can use the following docker command:
docker run -d -p 5432:5432 -e POSTGRES_DB=spring -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret postgres
Spring Boot provides a convenient way to start up an application context to be used in a test.
The @SpringBootTest
and @Testcontainers
annotations can help you to start your application with a database.
Other than that, the @DataJpaTest
meta-annotation contains the @Transactional
annotation.
This ensures our test execution is wrapped with a transaction that gets rolled back after the test.
This project was generated with Spring Initializr.
All of the app's code goes in a folder named src/main
.
The unit tests and e2e tests are in the src/test
and src/e2eTest
folders.
Static files are placed in src/main/resources
folder.
The tasks in build.gradle file were built with simplicity in mind to automate as much repetitive tasks as possible and help developers focus on what really matters.
The next tasks should be executed in a console inside the root directory:
./gradlew tasks
- Displays the tasks runnable from root project 'app'../gradlew bootRun
- Runs this project as a Spring Boot application../gradlew check
- Runs all checks../gradlew test
- Runs the unit tests../gradlew e2e
- Runs the end-to-end tests../gradlew clean
- Deletes the build directory../gradlew build
- Assembles and tests this project../gradlew help
- Displays a help message.
For more details, read the Command-Line Interface documentation in the Gradle User Manual.
Unit tests are responsible for testing of individual methods or classes by supplying input and making sure the output is as expected.
Use ./gradlew test
to execute the unit tests via JUnit 5, Mockito and AssertJ.
Use ./gradlew test -t
to keep executing unit tests in real time while watching for file changes in the background.
You can see the HTML report opening the index.html file in your web browser.
It's a common requirement to run subsets of a test suite, such as when you're fixing a bug or developing a new test case.
Gradle provides different mechanisms.
For example, the following command lines run either all or exactly one of the tests in the SomeTestClass
test case:
./gradlew test --tests SomeTestClass
For more details, you can see the Test filtering section of the Gradle documentation.
This project uses JaCoCo which provides code coverage metrics for Java. The minimum code coverage is set to 80%. You can see the HTML coverage report opening the index.html file in your web browser.
E2E tests determine if independently developed units of software work correctly when they are connected to each other.
Use ./gradlew e2e
to execute the e2e tests via JUnit 5, Testcontainers, Selenium 4 and AssertJ.
Use ./gradlew e2e -t
to keep executing your tests while watching for file changes in the background.
You can see the HTML report opening the index.html file in your web browser.
Like unit tests, you can also run subsets of a test suite. See the Test filtering section of the Gradle documentation.
You can debug the source code, add breakpoints, inspect variables and view the application's call stack.
Also, you can use the IDE for debugging the source code, unit and e2e tests.
You can customize the log verbosity of gradle tasks using the -i
or --info
flag.
For further reference, please consider the following articles: