When building web applications, end-to-end (E2E) testing is a key part of the development process. By definition, E2E testing is a type of testing that covers the entire application, from start to finish.
In a traditional web application, the front-end code would make a request to the back-end server, which would then process the request and return a response. With a Spring Boot application, the front-end code still makes a request, but the back-end server is now a Spring Boot application.
E2E testing is important because it allows developers to test the entire application, from the user interface (UI) to the back-end server. It also allows developers to test how the application works when it is deployed to a production environment.
There are many different E2E testing frameworks available, but for this article we will focus on Spring Boot and the E2E testing framework that it provides.
Spring Boot is a framework that allows developers to quickly create and deploy Spring-based applications. It provides a number of features that make development and deployment easier, such as:
Spring Boot is a great choice for developing E2E tests because it makes it easy to create and deploy a Spring-based application.
The E2E Testing Framework is a set of tools that allow developers to quickly create and run E2E tests. It is based on the JUnit testing framework and provides a number of features that make E2E testing easier, such as:
The E2E Testing Framework is a great choice for developing E2E tests because it makes it easy to create and run E2E tests.
Let's now look at how to create an E2E test using the E2E Testing Framework. We will be testing a simple Spring Boot application that has a single endpoint that returns a list of users.
The first thing we need to do is create a new JUnit test class and annotate it with @SpringBootTest:
@SpringBootTest
public class UserControllerTest {
}
The @SpringBootTest annotation is used to configure the Spring Boot application for testing.
Next, we need to create a test method and annotate it with @Test:
@Test
public void testGetUsers() {
}
The @Test annotation is used to mark a method as a test method.
In the test method, we need to create an instance of the UserController and invoke the getUsers() method:
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
}
The getUsers() method returns a list of users, so we can assert that the list is not empty:
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
assertThat(users).isNotEmpty();
}
We can also assert that the list contains the expected number of users:
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
assertThat(users).hasSize(2);
}
Finally, we can assert that the list contains the expected users:
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
assertThat(users)
.contains(new User("user1@example.com", "User 1"),
new User("user2@example.com", "User 2"));
}
The complete E2E test is shown below:
@SpringBootTest
public class UserControllerTest {
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
assertThat(users)
.contains(new User("user1@example.com", "User 1"),
new User("user2@example.com", "User 2"));
}
}
To run the E2E test, we simply need to run the test class as a JUnit test:
@SpringBootTest
public class UserControllerTest {
@Test
public void testGetUsers() {
UserController userController = new UserController();
List<User> users = userController.getUsers();
assertThat(users)
.contains(new User("user1@example.com", "User 1"),
new User("user2@example.com", "User 2"));
}
}
In this article, we have seen how to create and run an E2E test using the E2E Testing Framework. We have also seen how to use the @SpringBootTest and @Test annotations to configure the Spring Boot application and mark a method as a test method.