【问题标题】:getting error in spock test case while performing test for rest controller在对休息控制器执行测试时在 spock 测试用例中出错
【发布时间】:2019-11-30 03:42:39
【问题描述】:

我正在为我的控制器在 Spring Boot 中编写一个测试用例,下面是我的控制器

@RestController
public class HelloController {

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

现在我为同样的内容编写 Spock 测试用例,如下所示

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

但作为回应,我收到以下异常,请告知如何克服同样的问题 条件失败并出现异常:

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)

【问题讨论】:

  • 好吧,根据错误消息,mvc 为空,在您的测试中,我也看不到您声明该变量或属性的位置。去图...

标签: spring-mvc spock spring-mvc-test


【解决方案1】:

我根本不是 Spring 用户,但我确实认为您的问题更像是 Spring MVC 测试问题,而不是 Spock 问题。阅读一些tutorial 怎么样?在那里你会发现你应该

  • 自动连接MockMvc 实例,类似于您自己的示例代码中的控制器,
  • @AutoConfigureMockMvc 添加到您的测试类。

我想你可以通过阅读非常清晰的 Spock 错误消息来解决这个问题。正如我之前在评论中所说,它明确指出mvcnull。出于某种原因,您似乎希望成员变量在没有任何配置或接线的情况下神奇地存在。

以下是链接教程中的一些示例代码:

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

Spock documentation 中也可以找到 Spock 示例。

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2014-12-01
    • 2022-10-18
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多