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?
我是单元测试和嘲笑的新手。我试图在Grails中对抽象域类进行单元测试。我应该如何模拟实现,以便我可以单元测试域类的约束?有没有办法使用groovy或grails附带的模拟库?我应该只实现一个只扩展抽象类的类吗?
1 个解决方案
#1
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.
关于groovy(很多)的一个很酷的事情是你可以使用带闭包的方法名称映射作为模拟类的值。这包括抽象类。
abstract class Foo {
def foo() {
return bar() + 1
}
abstract int bar()
}
def fooInst = [bar: {-> return 1 }] as Foo
assert 2 == fooInst.foo()
#1
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.
关于groovy(很多)的一个很酷的事情是你可以使用带闭包的方法名称映射作为模拟类的值。这包括抽象类。
abstract class Foo {
def foo() {
return bar() + 1
}
abstract int bar()
}
def fooInst = [bar: {-> return 1 }] as Foo
assert 2 == fooInst.foo()