我的测试控制器这是一个带有spock框架的groovy类。
class LookupControllerSpec extends Specification {
def lookupService = Mock(LookupService)
def lookupController = new LookupController(lookupService)
MockMvc mockMvc = standaloneSetup(lookupController).build()
def "should return a single lookup record when test hits the URL and parses JSON output"() {
when:
def response = mockMvc.perform(get('/api/lookups/{lookupId}',2L)).andReturn().response
def content = new JsonSlurper().parseText(response.contentAsString)
then:
1 * lookupService.fetch(2L)
response.status == OK.value()
response.contentType.contains('application/json')
response.contentType == 'application/json;charset=UTF-8'
content.webId == 2L
}
}
错误:java.lang.illegalargumentexception:文本不能为null或空
我的java控制器
@RestController
@RequestMapping("/api/lookups")
@RequiredArgsConstructor
public class LookupController
{
@NonNull
private final LookupService lookupService;
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Page<LookupModel>> list(@RequestParam(required = false ) Long lookupId, Pageable pageable)
{
return ResponseEntity.ok(lookupService.fetchAll(lookupId,
pageable));
}
}
这对我很管用。
1条答案
按热度按时间20jt8wwn1#
首先,您需要向spockspring添加依赖项
然后你可以像这样用spring做集成测试:
现在只需指定模拟服务。