单元测试Groovy中的抽象类 [英] Unit testing Abstract classes in Groovy

查看:263
本文介绍了单元测试Groovy中的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试和嘲笑的新手。我尝试在Grails中单元测试一个抽象的域类。我应该如何模拟一个实现,以便我可以单元测试域类的约束条件?有没有办法使用groovy或grails附带的模拟库?我应该只实现一个简单地扩展抽象类的类吗?

解决方案

关于groovy(很多)的一个很酷的事情是你可以使用包含闭包的方法名称映射来模拟出一个类。这包括抽象类。

 抽象类Foo {
def foo(){
return bar() + 1
}

抽象的int bar()
}

def fooInst = [bar:{ - >返回1}]为Foo
断言2 == fooInst.foo()


I am new to unit testing and mocking. I am trying to unit test an abstract domain class in Grails. How should I mock an implementation so I can unit test the constraints of the domain class? Is there a way to use the mock libraries that come with groovy or grails? Should I just implement a class that simply extends the abstract class?

解决方案

One cool thing about groovy (among many) is that you can use a map of method names with closures as values to mock out a class. This includes abstract classes.

abstract class Foo {
    def foo() {
       return bar() + 1
    }    

    abstract int bar()
}

def fooInst = [bar: {-> return 1 }] as Foo
assert 2 == fooInst.foo()

这篇关于单元测试Groovy中的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆