Groovy Spock测试用例 - 内部方法调用

时间:2016-11-17 11:20:25

标签: unit-testing groovy spock testcase

ApplicationController.groovy:

public class ApplicationController { 
    @Autowired
    ApplicationService applicationService

    public void executeMethodA(Map requestBody, HttpServletRequest request, HttpServletResponse response) {
        String result = applicationService.methodB('one','two')
    }
}

ApplicationService.groovy:

public class ApplicationService {  
    public String methodB(String arg1, String arg2) {
        String concatString = arg1.concat(arg2)
        return concatString 
    }
}

Spock测试案例:

when:
applicationController.executeMehodA(requestBody, request, response)

then:
1 * applicationService.methodB('one','two') >> "onetwo"

控制器方法 - executeMethodA 服务类方法 - methodB 我们在Controller Test类中模拟了Service类:

ApplicationService applicationService = Mock(ApplicationService)

我在控制器中有一个方法调用服务类中的方法,我的Spock测试用例被传递。

ApplicationController.groovy:

public class ApplicationController { 
    @Autowired
    ApplicationService applicationService
    public void executeMethodA(Map requestBody, HttpServletRequest request, HttpServletResponse response) {
        String result = applicationService.methodC('one','two','three')
    }
}

ApplicationService.groovy:

public class ApplicationService {  
    public String methodB(String arg1, String arg2) {
        String concatString = arg1.concat(arg2)
        return concatString 
    }

    public String methodC(String d1, String d2, String d3) {
        String concatString = methodB(d1, d2)
        String finalString = methodB(concatString , d3)
        return finalString
    }
}

测试代码:

when:
applicationController.executeMethodA(requestBody, request, response)
then:
1 * applicationService.methodC('one','two','three') >> "onetwothree"

当控制器方法调用服务类中的方法时,该方法又调用另一个方法。 我们收到一个错误,调用不匹配为1 *或0 *

尝试使用1 *以及0 *。但我没有取得任何成功

错误讯息:

对以下内容的调用太少: 1 * applicationService.methodC(' one',' two'' three')>> " onetwothree" (0次调用)

不匹配的调用(按相似性排序): 1 * applicationService.this $ 2 $ methodB(' one',' two')

需要一些关于如何编写从另一个

调用方法的spock测试用例的指导

0 个答案:

没有答案