-1

I am writing a test case for my controller in spring boot below is my controller

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String helloWorld(){
        return "hello world";
    }
}

Now I writing the Spock test case for the same also as shown below

package groovy

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Title

@Title("Application Specification")
@Narrative("Specification which beans are expected")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HelloControllerTest extends Specification {
    @Autowired
    private HelloController helloController

    def "when get is performed then the response has status 200 and content is 'hello world'"() {
        expect: "Status is 200 and the response is 'hello world'"
        mvc.perform(get("/hello"))
                .andExpect(status().isOk())
                .andReturn()
                .response
    }
}

but in response I am getting the below exception please advise how to overcome from the same Condition failed with Exception:

mvc.perform(get("/hello")) .andExpect(status().isOk()) .andReturn() .response
|           |
null        groovy.lang.MissingMethodException: No signature of method: groovy.HelloControllerTest.get() is applicable for argument types: (java.lang.String) values: [/hello]
            Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), Spy(), any()
                at groovy.HelloControllerTest.when get is performed then the response has status 200 and content is 'hello world'(HelloControllerTest.groovy:26)


    at groovy.HelloControllerTest.when get is performed then the response has status 200 and content is 'hello world'(HelloControllerTest.groovy:27)
Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.HelloControllerTest.get() is applicable for argument types: (java.lang.String) values: [/hello]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), Spy(), any()
    at groovy.HelloControllerTest.when get is performed then the response has status 200 and content is 'hello world'(HelloControllerTest.groovy:26)
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Well, `mvc` is null according to the error message and in your test I also cannot see where you declare that variable or property. Go figure... – kriegaex Jul 22 '19 at 16:16

1 Answers1

1

I am not a Spring user at all, but I do think your problem is more a Spring MVC testing problem than a Spock problem as such. How about reading some tutorial? There you will find that you should

  • auto-wire the MockMvc instance similar to the controller in your own sample code,
  • add @AutoConfigureMockMvc to your test class.

I think you could have figured this out by just reading the wonderfully clear Spock error message. It clearly states that mvc is null, as I said earlier in my comment. For some reason you seem to expect the member variable just to magically exist without any configuration or wiring.

Here is some sample code taken from the linked tutorial:

package hello;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString("Hello World")));
    }
}

A Spock example can be found in the Spock documentation, too.

kriegaex
  • 63,017
  • 15
  • 111
  • 202