Grails Gmock partial mock example

 
class Foo {
 
    def methodFoo(String param){
        return "methodFoo"
    }
}
 
...
 
class Bar {
 
    def foo
 
    def methodBar(String param){
        return foo.methodFoo(param) + methodBarSecond(param);
    }
 
    def methodBarSecond(String param){
         return "methodBarSecond"
    }
}
 
...
 
import static org.hamcrest.Matchers.any
 
@WithGmock
class BarTests {
 
    @Test
    public void checkMethodBar(){
        def result = "my other result"
        def mockFoo = mock(Foo)
        mockFoo.testMethod(any(String)).returns(result)
 
        Bar bar = new Bar()
        bar.foo = mockFoo
        mock(bar).methodBarSecond(any(String)).returns(result)
 
        play {
            assertEquals (result * 2, bar.methodBar())
        }
    }
}
 

No Comments yet »

RSS feed for comments on this post.

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.