MockMvc bean auto-injection does not work in Spring Boot 2.7.0. So NPE happens.

@AutoConfigureMockMvc
@SpringBootTest
class UserServiceImplTest {

    @Autowired
    MockMvc mockMvc;

// id 'org.springframework.boot' version '2.7.0'
Unsatisfied dependency expressed through field 'mockMvc'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Comment From: bclozel

Can you provide a sample application that reproduces the problem? Ideally something we can git clone (or a zip file as an attachment here). We do have tests checking for this case and we need more information to understand what's happening. Thanks!

Comment From: cwangg897

Can you provide a sample application that reproduces the problem? Ideally something we can git clone (or a zip file as an attachment here). We do have tests checking for this case and we need more information to understand what's happening. Thanks!

I set auto-injection dependency to id 'org.springframework.boot' version '2.7.0' as implementation group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.6.3' It seems to have been an error. It's my fault. But it's not a Spring Boot problem.

SpringBoot spring-boot 2.7.0 MockMvc DI SpringBoot spring-boot 2.7.0 MockMvc DI

In IntelliJ, in spring boot 2.7.0 version, there is only a problem that @Autowired is MockMvc or a red underline while writing the test code, and it is not a problem of Spring boot.

Comment From: bclozel

OK, thanks for letting us know. I'm glad this is working now for you.