0
0
Spring Bootframework~5 mins

@SpringBootTest for integration tests

Choose your learning style9 modes available
Introduction

@SpringBootTest helps you run your whole Spring Boot app in a test. It makes sure all parts work together, not just small pieces.

When you want to test if your app starts correctly with all settings.
When you need to check if different parts of your app work together.
When you want to test your database connection and data access.
When you want to test your REST controllers with real HTTP calls.
When you want to verify your app’s full behavior, not just one class.
Syntax
Spring Boot
@SpringBootTest
public class YourTestClass {

    @Test
    void testSomething() {
        // test code here
    }
}

This annotation tells Spring Boot to start the full application context for tests.

You can add properties or specify web environment if needed.

Examples
Basic test to check if Spring Boot app starts without errors.
Spring Boot
@SpringBootTest
public class SimpleIntegrationTest {

    @Test
    void contextLoads() {
        // test if app context starts
    }
}
Starts app with a random port to test web endpoints.
Spring Boot
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebIntegrationTest {

    @LocalServerPort
    private int port;

    @Test
    void testHttpCall() {
        // use port to call REST endpoints
    }
}
Overrides properties for testing with an in-memory database.
Spring Boot
@SpringBootTest(properties = {"spring.datasource.url=jdbc:h2:mem:testdb"})
public class DatabaseIntegrationTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    void testDatabase() {
        // test database access
    }
}
Sample Program

This test starts the full Spring Boot app on a random port. It uses RestTemplate to call the /greeting endpoint and checks if the response contains "Hello, World".

Spring Boot
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingIntegrationTest {

    @LocalServerPort
    private int port;

    @Autowired
    private RestTemplate restTemplate;

    @Test
    void greetingShouldReturnDefaultMessage() {
        String url = "http://localhost:" + port + "/greeting";
        String response = restTemplate.getForObject(url, String.class);
        assertThat(response).contains("Hello, World");
    }
}
OutputSuccess
Important Notes

Use @SpringBootTest only for integration tests because it starts the whole app and can be slow.

For faster tests of small parts, use @WebMvcTest or @DataJpaTest instead.

Remember to clean up or isolate data when testing with databases.

Summary

@SpringBootTest runs your full Spring Boot app for testing.

It helps check if all parts work together correctly.

Use it mainly for integration tests, not small unit tests.