Spock中的MockMvc无法正常工作

teh*_*ras 5 junit spring mockito spock mockmvc

我有一个简单的控制器设置:

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<String> getTestString(){
        List<String> sampleTest = new ArrayList<String>();
        sampleTest.add("Test");

        return sampleTest;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于这个简单的控制器,我正在尝试使用MockMVC在Spock中编写测试:

class TestControllerTest extends Specification {

    MockMvc mockMvc;

    def setup(){
        mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build();
    }

    def "testing TestController"(){
        when:
        MvcResult response = mockMvc.perform(get("/test/1"));

        then:
        response.andExpect(content().string('["Test"]'));
    }
}
Run Code Online (Sandbox Code Playgroud)

我有的JAR是:

Spring-test:4.0.5 Javax-servlet-api:3.0.1 spock-spring:0.7-groovy-2.0

运行测试后得到的错误是这样的:

groovy.lang.MissingMethodException: No signature of method: com.crmservice.controller.TestControllerTest.get() is applicable for argument types: (java.lang.String) values: [/test]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), Spy(), any()
    at com.crmservice.controller.TestControllerTest.testing TestController(TestControllerTest.groovy:27)
Run Code Online (Sandbox Code Playgroud)

Opa*_*pal 10

是否get导入了丢失的方法?

您需要导入块中的以下行:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
Run Code Online (Sandbox Code Playgroud)