Groovy MockFor:使用多个模拟时如何避免嵌套的闭包?

时间:2019-04-26 09:15:16

标签: unit-testing groovy mocking clean-architecture

我有三节课:

class B1 {
    def performB1(){}
}

class B2 {
    def performB2(){}
}

class A {
    private B1 b1
    private B2 b2

    A(b1, b2){
        this.b1 = b1
        this.b2 = b2
    }

    def perfromA(){
        b1.performB1()
        b2.performB2()
    }
}

我想在类performA中测试方法A。因此,我为类B1B2创建了模拟程序。这是我的课程:

import groovy.mock.interceptor.MockFor;

 class ATest  extends GroovyTestCase {
     private MockFor b1Mock
     private MockFor b2Mock

    void setUp() {
        b1Mock = new MockFor(B1)
        b2Mock = new MockFor(B2)
    }


    void testIsEnoughSpaceOnArtifactory_failedToGetQuotaFromArtifactory(){
        b1Mock.demand.with {
            performB1 { println "Performing B1" }
        }

        b2Mock.demand.with {
            performB2 {println "Performing B2"}
        }

        b2Mock.use {
           b1Mock.use {
               def a = new A(new B1(), new B2())
               a.perfromA()
           }
        }
    }
}

效果很好-我验证了。它基于this question

但是,假设我有一个带有三个依赖项的类。它仍然是干净的代码。它需要3个模拟。代码如下所示:

b3Mock.use { 
    b2Mock.use {
        b1Mock.use {
            def a = new A(new B1(), new B2(), new B3())
            a.perfromA()
        }
    }
}

它看起来很荒谬,而且还很不干净。想象一下,我没有达到不超过3个依赖关系的目标。然后,我的测试将变得更加荒谬。有没有办法在没有嵌套闭包的情况下验证对模拟的调用?我可以使用类似的东西(请参阅here作为参考):

b1Mock.use { 
    def a = new A(new B1(), b2Mock.proxyInstance(), b3Mock.proxyInstance())
    a.perfromA()
}
b2Mock.expect.verify()
b3Mock.expect.verify()

不幸的是,当我运行它时,出现以下错误:

  

java.lang.NullPointerException:无法在空对象上调用方法performB2()

是否可以在没有嵌套闭包的情况下在Groovy中使用多个模拟来获得清晰的代码?

1 个答案:

答案 0 :(得分:3)

以下脚本可以正常工作:

import groovy.mock.interceptor.MockFor
//------ CLASSES
class B1 { def performB1(){} }
class B2 { def performB2(){} }
class B3 { def performB3(){} }

class A {
    private B1 b1
    private B2 b2
    private B3 b3

    A(b1, b2, b3){
        this.b1 = b1
        this.b2 = b2
        this.b3 = b3
    }

    def perfromA(){
        b1.performB1()
        b2.performB2()
        b3.performB3()
    }
}
//------ TESTS
def b1Mock = new MockFor(B1)
def b2Mock = new MockFor(B2)
def b3Mock = new MockFor(B3)

b1Mock.demand.with {
    performB1 { println "Performing B1" }
}
b2Mock.demand.with {
    performB2 {println "Performing B2"}
}
b3Mock.demand.with {
    performB3 {println "Performing B3"}
}

def b2inst = b2Mock.proxyInstance()
def b3inst = b3Mock.proxyInstance()
b1Mock.use { 
    def a = new A(new B1(), b2inst, b3inst)
    a.perfromA()
}
b2Mock.verify(b2inst)
b3Mock.verify(b3inst)

并具有以下功能

def useAll(List<MockFor> mocks,Closure test){
    Closure use4all = mocks.inject(test){ Closure testX, next-> 
        return { next.use(testX) } 
    }
    use4all.call()
}

可能将对此的嵌套使用率降至最低:

useAll([b1Mock,b2Mock,b3Mock]){
    def a = new A(new B1(), new B2(), new B3())
    a.perfromA()
}