Home > Net >  autowore mockmvc is null in controller test after I upgrade spring framework to 6.0.1
autowore mockmvc is null in controller test after I upgrade spring framework to 6.0.1

Time:01-18

using: springboot version is 3.0.1 spring framework version 6.0.1 java version 17

after I upgrade my spring framework version, I refactor security configuration to this

@WebMvcTest(NominationController.class)
@AutoConfigureMockMvc
class NominationIsolationTest extends Specification {

  @Autowired
  MockMvc mockMvc

  ...

  def "should return status created with response when post nominations given valid nomination"() {
    given:
    ...

    when:
    def response = mockMvc")
                    .content(body).contentType(MediaType.APPLICATION_JSON))

    then:
    ...
  }

but it failed, and I debug find that the mockMvc' value is null, and I use spock to do the controller test

I have try to add @SpringBootTest annotation, but not work

before I upgrade, the controller test can run successfully

CodePudding user response:

now I fixed this issue, by add @ContextConfiguration annotation on test class, and autowire WebApplicationContext then use mvcbuilder to create mockmvc, after that, it fixed

@WebMvcTest
@ContextConfiguration
class IsolationTest extends Specification {

  @Autowired
  WebApplicationContext wac

  MockMvc mockMvc

  def setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()
  }
  • Related