Java Spring Boot Testing
In this page:
Integration Testing in Spring Boot
Integration tests verify that multiple pieces of a Spring Boot application -- database layers, configuration, and API controllers -- actually work correctly together in a realistic, wired-up context, catching bugs that isolated unit tests with mocked dependencies would miss entirely.
Example: Integration Testing in Spring Boot
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest // wires up the full, realistic application context
public class Main {
public static void main(String[] args) {
System.out.println("Integration tests catch bugs isolated unit tests would miss");
}
}
Login to try C/C++/Java/PHP code in the editor
Testing REST Controllers (MockMvc)
MockMvc lets you perform simulated HTTP requests against your controllers inside a test, exercising the full request-handling pipeline (routing, validation, serialization) without actually starting a real web server or opening a network socket.
Example: Testing REST Controllers (MockMvc)
import org.springframework.test.web.servlet.MockMvc;
public class Main {
public static void main(String[] args) {
// mockMvc.perform(get("/users/1")).andExpect(status().isOk());
System.out.println("MockMvc simulates HTTP requests without a real server");
}
}
Login to try C/C++/Java/PHP code in the editor
Mocking with @MockBean
@MockBean is Spring's test-specific way to swap out a real Bean, like a database repository, for a Mockito mock inside the application context used by a test. This lets you isolate the controller or service under test from a dependency you don't want to hit for real.
Example: Mocking with @MockBean
import org.springframework.boot.test.mock.mockito.MockBean;
public class Main {
interface UserRepository { String findById(int id); }
@MockBean
UserRepository userRepository; // swaps the real Bean for a Mockito mock in the test context
public static void main(String[] args) {
System.out.println("@MockBean isolates the class under test from a real dependency");
}
}
Login to try C/C++/Java/PHP code in the editor
Database Slice Testing
@DataJpaTest isolates just the JPA-related components of your application and boots a fast, in-memory database configuration automatically, which is far quicker than spinning up your full application context just to validate repository query behavior.
Example: Database Slice Testing
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
@DataJpaTest // boots only the JPA layer with a fast in-memory database
public class Main {
public static void main(String[] args) {
System.out.println("Faster than booting the full application context");
}
}
Login to try C/C++/Java/PHP code in the editor
Mockito Verification Tests
Mockito's verification features let you confirm that a specific mocked method was actually called during a test, and track how many times it happened -- useful for asserting on side effects (like 'was the email service invoked') that a return value alone wouldn't reveal.
Example: Mockito Verification Tests
import static org.mockito.Mockito.*;
public class Main {
interface EmailService { void send(String to); }
public static void main(String[] args) {
EmailService mockEmail = mock(EmailService.class);
mockEmail.send("[email protected]");
verify(mockEmail).send("[email protected]"); // confirms the side effect happened
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: