0
0
SpringbootHow-ToBeginner · 4 min read

How to Test Spring Boot Application: Simple Guide with Examples

To test a Spring Boot application, use @SpringBootTest for integration tests and MockMvc for web layer tests. Write tests with JUnit 5 and use @Test methods to verify your application behavior.
📐

Syntax

Use @SpringBootTest to load the full application context for integration tests. Use @AutoConfigureMockMvc to enable MockMvc for testing web endpoints without starting a server. Write test methods with @Test annotation from JUnit 5.

  • @SpringBootTest: Loads the full Spring context.
  • @AutoConfigureMockMvc: Configures MockMvc for HTTP request testing.
  • MockMvc: Simulates HTTP requests to controllers.
  • @Test: Marks a method as a test case.
java
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@SpringBootTest
@AutoConfigureMockMvc
public class MyApplicationTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void exampleTest() throws Exception {
        mockMvc.perform(get("/api/hello"))
               .andExpect(status().isOk())
               .andExpect(content().string("Hello World"));
    }
}
💻

Example

This example shows a simple Spring Boot test that checks if the /api/hello endpoint returns "Hello World" with HTTP status 200.

java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
class HelloController {
    @GetMapping("/api/hello")
    public String hello() {
        return "Hello World";
    }
}

// Test class
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.junit.jupiter.api.Test;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@SpringBootTest
@AutoConfigureMockMvc
public class DemoApplicationTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void helloEndpointReturnsHelloWorld() throws Exception {
        mockMvc.perform(get("/api/hello"))
               .andExpect(status().isOk())
               .andExpect(content().string("Hello World"));
    }
}
Output
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
⚠️

Common Pitfalls

Common mistakes when testing Spring Boot applications include:

  • Not using @SpringBootTest or missing @AutoConfigureMockMvc, causing tests to fail due to missing context or beans.
  • Starting the full server instead of using MockMvc, which slows tests.
  • Not handling exceptions in tests, leading to false negatives.
  • Using legacy JUnit 4 annotations instead of JUnit 5.
java
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/* Wrong way: Missing @AutoConfigureMockMvc causes NullPointerException */
@SpringBootTest
public class WrongTest {
    @Autowired
    private MockMvc mockMvc; // This will be null

    @Test
    void test() throws Exception {
        mockMvc.perform(get("/api/hello")); // NullPointerException
    }
}

/* Right way: Add @AutoConfigureMockMvc to enable MockMvc injection */
@SpringBootTest
@AutoConfigureMockMvc
public class RightTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    void test() throws Exception {
        mockMvc.perform(get("/api/hello"))
               .andExpect(status().isOk());
    }
}
📊

Quick Reference

Summary tips for testing Spring Boot applications:

  • Use @SpringBootTest for full context integration tests.
  • Use MockMvc with @AutoConfigureMockMvc for fast web layer tests.
  • Write tests with JUnit 5 @Test methods.
  • Mock external dependencies to isolate tests.
  • Run tests frequently to catch issues early.

Key Takeaways

Use @SpringBootTest and @AutoConfigureMockMvc to test Spring Boot applications effectively.
MockMvc allows testing web endpoints without starting the full server.
Write test methods with JUnit 5 @Test annotation for clear and modern tests.
Avoid missing annotations that cause NullPointerExceptions in tests.
Mock external services to keep tests fast and reliable.