如何在Spock测试框架中两次测试调用两次get方法

时间:2019-03-28 09:57:04

标签: unit-testing groovy spock

我是Spock的新手。现在我在源Java方法中有两次get方法,我编写了代码,但始终会得到NP异常。任何人都可以帮助查看。

    def "check call outMethid2"(){
        given:
        def mockAppointRuleService = Mock(IAppointRuleService)
        def mockService = Mock(AppDomainContext)
        mockService.getBean(_,_ as Class) >> mockAppointRuleService
        def userDTO = new UserDTO(name: "chendd")
        mockAppointRuleService.usxx(_) >>  userDTO

        when:
        IAppointRuleService appointRuleService = AppDomainContext.getBean("eh.appointRuleService", IAppointRuleService.class)
        def resutl = appointRuleService.usxx()

        then:
        resutl == userDTO
    }

我希望获得userDTO,但也希望获得nullpointeException

1 个答案:

答案 0 :(得分:0)

据我所见,您应该在getBean(...)上调用mockService而不是在AppDomainContext块中的when:上调用。另外,除非是综合示例,否则我不太了解此处要测试的内容。或者您正在测试Spock模拟功能。换句话说,这里缺少测试中的代码(行为)