繁体   English   中英

mockMvc.perform 上的错误。 Null 指针异常

[英]Error On mockMvc.perform. Null Pointer Exception

我的测试 Controller 这是一个带有 Spock 框架的 Groovy class。

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 Controller

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

这对我来说很好。

首先,您需要向 spock-spring 添加依赖项

testImplementation('org.spockframework:spock-spring:2.0-M1-groovy-2.5')
testImplementation('org.spockframework:spock-core:2.0-M1-groovy-2.5')

testImplementation('org.springframework.boot:spring-boot-starter-test') {
   exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

然后您可以像这样使用 spring 进行集成测试:

import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import spock.lang.Specification

import javax.servlet.http.HttpServletResponse

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

@AutoConfigureMockMvc
@WebMvcTest
class LookupControllerTest extends Specification {

    @Autowired
    private MockMvc mockMvc

    @SpringBean
    private LookupService lookupService = Mock()

    def "should return a single lookup record when test hits the URL and parses JSON output"() {
        when:
        def response = mockMvc
                .perform(get('/api/lookups/')
                        .param("lookupId", "2")
                        .param("page", "0")
                        .param("size", "0"))
                .andReturn()
                .response

        then:
        response.status == HttpServletResponse.SC_OK
    }
}

您只需要现在指定您的 Mock 服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM