MockMvc in Spock not working
Asked Answered
F

1

6

I have a setup of a simple controller:

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

For this simple controller I'm trying to write a test in Spock using MockMVC:

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"]'));
    }
}

The JARs I have are:

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

The Error I get after running the test is this:

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)
Firebrand answered 5/12, 2014 at 3:52 Comment(0)
D
12

Is the missing get method imported?

You need the following line in imports block:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
Digiacomo answered 5/12, 2014 at 7:57 Comment(1)
tnx, this was the issue :)Firebrand

© 2022 - 2024 — McMap. All rights reserved.