← Back to Advanced Java Course | Chapter 10: Spring Boot | Lesson 5 of 6

Java Spring Boot Testing

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

java
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");
	}
}

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)

java
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");
	}
}

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

java
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");
	}
}

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

java
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");
	}
}

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

java
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
	}
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.