【问题标题】:MockMvc in Spock not workingSpock中的MockMvc不起作用
【发布时间】:2014-12-05 03:52:13
【问题描述】:

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

@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;
    }
}

对于这个简单的控制器,我正在尝试使用 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"]'));
    }
}

我拥有的 JAR 是:

弹簧测试: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)

【问题讨论】:

    标签: spring junit mockito spock mockmvc


    【解决方案1】:

    是否导入了缺少的get 方法?

    您需要在导入块中添加以下行:

    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
    

    【讨论】:

    • tnx,这就是问题所在:)
    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 2016-12-11
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多