我有一个服务类有2种方法(一种是公共的,另一种是保护的)。我想单元测试我的代码。下面是代码片段 MyServiceClass.groovy模拟单元测试下的服务类中的方法
class MyServiceClass {
public boolean myMethod1() {
// some code
boolean success = myMethod2()
// some code
}
public boolean myMethod2() {
// some logic to return true/false
return true
}
}
MyServiceClassTests.groovy
class MyServiceClassTests {
void testMyMethod1() {
// unit test code
}
}
在上面的单元测试代码,我想嘲笑其通过myMethod1(),即调用myMethod2()的返回结果,这两种方法都在同一服务类正在进行单元测试。如何嘲笑它并完成任务?
为什么我们不能这样做..? – 2014-09-23 15:04:01
因为你必须在你的测试中实例化一个新的MyServiceClass(),所以你正在执行“真实”的代码来测试。因此,同一个类不能成为模拟对象。 – Brad 2014-09-23 15:11:24
是他们的任何其他方式来解决这个iisue而不移动方法到任何一个类...? – 2014-09-23 15:15:02