Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Abstract classes in Groovy

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?

like image 844
Matthew Sowders Avatar asked Feb 03 '23 10:02

Matthew Sowders


1 Answers

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()
like image 66
Ted Naleid Avatar answered Feb 11 '23 07:02

Ted Naleid