Issues with injecting beans like MockMvc when using spring boot and spock

46 views Asked by At

I have a spring boot application, with the below lines to setup testing using spock

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.spockframework:spock-spring:2.0-groovy-3.0'
    testImplementation 'org.codehaus.groovy:groovy-all:3.0.7'

Next, I have a test class where I noticed that the autowired classes like MockMvc, WebApplicationContext where not getting injected. Below is my test class


import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.web.servlet.MockMvc
import spock.lang.Specification

@SpringBootTest
@AutoConfigureMockMvc
class TestControllerSpec extends Specification {

    @Autowired
    MockMvc mvc;

    def "test bean injection"() {
        expect:
        mvc != null
    }
}

I want the above test to pass. Initially, I tried to run the test using mockito and that worked but I had to switch to the spock framework for readability.

0

There are 0 answers