0
votes

I am in the initial stage of spock and groovy, I am trying to test one simple spring boot application and getting

Below is my java and groovy code with exception details

Condition not satisfied:

mvc.perform(get('/hello')).andExpect(status().isOk())
|           |
|           groovy.lang.MissingMethodException: No signature of method: org.spockframework.controllers.TestControllerSpec.get() is applicable for argument types: (java.lang.String) values: [/hello]
|           Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), Spy(), getMvc()
org.springframework.test.web.servlet.MockMvc@6bb68f5

at org.spockframework.controllers.TestControllerSpec.spring context loads for web mvc slice(TestControllerSpec.groovy:28)
Caused by: groovy.lang.MissingMethodException: No signature of method: org.spockframework.controllers.TestControllerSpec.get() is applicable for argument types: (java.lang.String) values: [/hello]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), Spy(), getMvc()

and above exception comes when some argument is missing don't what argument and how to pass argument below is my code

TestController.java

@RequestMapping(value = "/hello")
@ResponseBody
public String sayHelloWorld() {
    return "hello world";
}

TestControllerSpec.groovy

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders

import spock.lang.Specification
import spock.lang.Unroll
import spock.mock.DetachedMockFactory

@ContextConfiguration
@WebMvcTest(TestController)
class TestControllerSpec extends Specification {

 @Autowired
 MockMvc mvc;

 @Unroll
 def "spring context loads for web mvc slice"() {

    expect: "controller is available"
    mvc.perform(get('/hello')).andExpect(status().isOk())
 }

 @TestConfiguration
 static class MockConfig {
    def detachedMockFactory = new DetachedMockFactory();
 }
}

I am sure my code is missing something

1
Please provide imports for Spec class. I think that you might forgot to import some method (get() or status()). - Jakub Dyszkiewicz
@JakubDyszkiewicz I have update my code. - pise

1 Answers

0
votes

You didn't import method get. Try to add it

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

Groovy is dynamic language so it let's you run your code even if you forgot your imports, because you might attach methods to class in runtime. Always look on warnings in your IDE when your writting in Groovy (in any other languages too, but especially in Groovy).