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.  

Grails bean factory + method with parameters

  1.  
  2. class MyObjectFactory {
  3. static final int TYPE_ONE = 1
  4. static final int TYPE_TWO = 2
  5.  
  6. def produce(int type){
  7. if (type == TYPE_ONE){
  8. return ... // type one
  9. }
  10.  
  11. if (type == TYPE_TWO){
  12. return ... // type two
  13. }
  14.  
  15. }
  16. }
  17.  
  18. ...
  19.  
  20. beans {
  21.  
  22. myObjectFactoryBean(MyObjectFactory)
  23.  
  24. myObjectBean(myObjectFactoryBean:'produce'){bean->
  25. bean.constructorArgs = [MyObjectFactory.TYPE_ONE ]
  26. }
  27. }
  28.  

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