Grails Gmock partial mock example

  1.  
  2. class Foo {
  3.  
  4. def methodFoo(String param){
  5. return "methodFoo"
  6. }
  7. }
  8.  
  9. ...
  10.  
  11. class Bar {
  12.  
  13. def foo
  14.  
  15. def methodBar(String param){
  16. return foo.methodFoo(param) + methodBarSecond(param);
  17. }
  18.  
  19. def methodBarSecond(String param){
  20. return "methodBarSecond"
  21. }
  22. }
  23.  
  24. ...
  25.  
  26. import static org.hamcrest.Matchers.any
  27.  
  28. @WithGmock
  29. class BarTests {
  30.  
  31. @Test
  32. public void checkMethodBar(){
  33. def result = "my other result"
  34. def mockFoo = mock(Foo)
  35. mockFoo.testMethod(any(String)).returns(result)
  36.  
  37. Bar bar = new Bar()
  38. bar.foo = mockFoo
  39. mock(bar).methodBarSecond(any(String)).returns(result)
  40.  
  41. play {
  42. assertEquals (result * 2, bar.methodBar())
  43. }
  44. }
  45. }
  46.  
  47.  

No Comments yet

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

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