Menu

@MockBean Is Gone: Fixing Your Tests After Upgrading to Spring Boot 4
πŸ“°

@MockBean Is Gone: Fixing Your Tests After Upgrading to Spring Boot 4

DEV Community: javaΒ·Pushpak RautΒ·9 days ago
#vI44YpS6
#devtojava#devto#dev#test#boot
Long article (1112 words) β€” no AI summary yet.
Reading 0:00
15s threshold

You bump the Spring Boot version to 4, run the build, and the main code compiles fine. Then the tests fail with a wall of errors like these:

cannot find symbol

symbol: class MockBean

package org.springframework.boot.test.autoconfigure.web.servlet does not exist

The test code itself is fine. Spring Boot 4 removed @MockBean and @SpyBean, split the test starter into one starter per technology, and moved the slice annotations like @WebMvcTest and @DataJpaTest to new packages.

Once you know the new names, the fixes are mostly search and replace. Below is each one, shown on a small Books app with three tests that pass on Spring Boot 4.1.2.

The app under test

A Book entity, a repository, a service and a controller with one endpoint:

`@RestController

@RequestMapping("/books")

public class BookController {

private final BookService service;

public BookController(BookService service) { this.service = service; }

@GetMapping("/{id}") public Book get(@PathVariable long id) { return service.get(id);…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.