I'm trying to test a couple of controller endpoints with MockMvc and I'm having a bit of trouble (be kind, I'm new...). The simple endpoint that consumes a string as it's parameter works fine, but the slightly more complicated endpoint that consumes a list of strings is not happy and throws an exception; can anybody point out what I'm doing wrong?
@RestController
@RequestMapping("/bleh")
public class Controller
{
@Autowired
private DataService dataService
@RequestMapping(value = "/simple", method = RequestMethod.GET)
public String simple(String name)
{
return dataService.getSomeData(name)
}
@RequestMapping(value = "/complicated", method = RequestMethod.GET)
public String complex(List<String> names)
{
return dataService.getSomeOtherData(names)
}
}
-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HealthControllerTests extends Specification
{
def dataServiceMock;
def testController;
def mockMvc;
def setup(){
dataServiceMock = Mock(DataService)
dataServiceMock.getSomeData >> "blaah"
testController = new Controller(dataService: dataServiceMock)
mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
}
def "working test"
when:
def response = MockMvc.perform(get("/simple").param("name", "tim"))
.andReturn()
.getResponse();
then:
response.status == OK.value();
response.contentAsString == "blaah"
def "unhappy test"
when:
def response = MockMvc.perform(get("/complicated").param("names", new ArrayList<>()))
.andReturn()
.getResponse();
then:
response.status == OK.value()
}
throws this:
No signature of method: org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [names, []]. Possible solutions: param(java.lang.String, [Ljava.lang.String;), params(org.springframework.util.MultiValueMap), wait(), grep(), any(), putAt(java.lang.String, java.lang.Object)])