Home > Java > javaTutorial > How to build unit and integration tests using Spring Boot and JUnit

How to build unit and integration tests using Spring Boot and JUnit

WBOY
Release: 2023-06-23 08:39:06
Original
866 people have browsed it

With the continuous development of the software development industry, unit testing and integration testing have attracted more and more attention from developers. These testing methods can help developers quickly find problems in the code and improve software quality.

Spring Boot is a very popular Java development framework that helps developers quickly build applications. JUnit is a unit testing framework for the Java programming language that helps developers write efficient unit tests. In this article, I will explain how to build unit and integration tests using Spring Boot and JUnit.

  1. Unit testing

Unit testing is to test the smallest unit of code in the application. In the world of Java programming, unit testing usually tests a method or a class. In a Spring Boot application, unit tests can test components such as controllers, services, repositories, etc.

1.1 Create a Spring Boot application

First, we need to create a Spring Boot application. The following is a simple example:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
Copy after login

@SpringBootApplication annotation is the core annotation of Spring Boot context, which includes @EnableAutoConfiguration and @ComponentScan annotations. This sample application has only one main method.

1.2 Writing unit tests

Next, we will write a simple unit test to test the methods in a class.

public class GreetingServiceTest {

    private GreetingService greetingService;

    @Before
    public void setUp() {
        greetingService = new GreetingService();
    }

    @Test
    public void testGreeting() {
        String result = greetingService.greet("John");
        assertEquals("Hello John", result);
    }
}
Copy after login

This is a very simple unit test example. We created a GreetingServiceTest class and wrote a testGreeting method in it. In the setUp method, we create a GreetingService object. In the testGreeting method, we call the greetingService.greet("John") method and use the assertEquals method to verify whether the result is "Hello John".

1.3 Running Unit Tests

In order to run unit tests, we need to right-click the test class in the IDE and select "Run Test". After the test completes, we will see the results of the test run.

  1. Integration testing

Integration testing is testing a certain part of the entire application or the entire application. In Spring Boot, we can use SpringBootTest annotations to write integration tests.

2.1 Create an integration test

First, we need to create an integration test class.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingControllerIntegrationTest {
Copy after login

@RunWith and @SpringBootTest annotations are the core annotations of Spring Boot integration testing. The webEnvironment attribute is the environment of the Spring Boot application, which can be MOCK, RANDOM_PORT or DEFINED_PORT. In this example, we selected RANDOM_PORT. This example will test our GreetingController class. We will use Spring Boot TestRestTemplate in our tests to call the API and test the results.

2.2 Writing an integration test

Next, we will write a simple integration test to test our GreetingController.

@Test
public void testGreeting() {
    ResponseEntity<String> response = new TestRestTemplate().getForEntity(
            "http://localhost:"+ port +"/greeting?name=John", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("Hello John", response.getBody());
}
Copy after login

In this example, we use TestRestTemplate to call the API interface. We use the getForEntity method to get the response and the assertEquals method to verify that the result is "Hello John".

2.3 Run Integration Test

In order to run the integration test, we need to right-click the test class in the IDE and select "Run Test". After the test completes, we will see the results of the test run.

Summary

In this article, we introduced how to use Spring Boot and JUnit to build unit and integration tests. Unit testing is a testing method for testing the smallest unit of code in an application. Integration testing is a testing method that tests the entire application or a part of it. Unit testing and integration testing can help us find problems in the code and improve the quality of the application.

The above is the detailed content of How to build unit and integration tests using Spring Boot and JUnit. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template