{
  "id": 22015285,
  "title": "@MockBean Is Gone: Fixing Your Tests After Upgrading to Spring Boot 4",
  "url": "https://urgent.news/2026/11/26/mockbean-is-gone-fixing-your-tests-after-upgrading-to-spring-boot-4",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-26T17:40:29.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/pushpak_raut/mockbean-is-gone-fixing-your-tests-after-upgrading-to-spring-boot-4-4pb6"
  },
  "original_language": "en",
  "account": "When upgrading to Spring Boot 4, you may encounter issues with your existing tests. The main changes involve the removal of @MockBean and @SpyBean, the splitting of the test starter into separate starters for each technology, and the relocation of slice annotations to new packages. Here are the steps to address these changes:\n\n1. Update test dependencies: In Spring Boot 4, each technology now has its own test starter. For example, to test a web controller, you'll need to add spring-boot-starter-webmvc-test and spring-boot-starter-data-jpa-test to your project's dependencies. These starters include JUnit, AssertJ, and Mockito, so you don't need to include spring-boot-starter-test alongside them.\n\n2. Replace @MockBean with @MockitoBean: In Spring Boot 4, @MockBean has been moved to the Spring Framework (org.springframework.test.context.bean.override.mockito). To use it, simply change @MockBean to @MockitoBean on the field where you want to inject a mock object, and stub the mock using when(...) as before.\n\n3. Adjust test imports: The WebMvcTest annotation has moved from org.springframework.boot.test.autoconfigure.web.servlet to org.springframework.boot.webmvc.test.autoconfigure. Replace imports from the old package with those from the new one. Additionally, you may need to change the test runner from @RunWith(MockMvc.class) to @ExtendWith(MockitoExtension.class) when using JUnit 5.\n\n4. Update @SpyBean usage: When using @SpyBean, it's now necessary to use @MockitoSpyBean instead. This allows you to wrap the real bean and verify calls while still running real code. The rest of the test code remains the same.\n\n5. Be aware of moved test annotations: @DataJpaTest and TestEntityManager have been moved to different packages in Spring Boot 4. Update your imports accordingly to ensure your tests compile without errors.",
  "summary": "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…",
  "key_points": [
    "Update test dependencies: Add separate starters for each technology in Spring Boot 4.",
    "Replace @MockBean with @MockitoBean: Move @MockBean to Spring Framework and stub mock objects.",
    "Adjust test imports: Move WebMvcTest annotation to new package and change test runner for JUnit 5."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}