【发布时间】: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