Advertisement
dmahapatro

Spock Test

Jul 1st, 2013
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.51 KB | None | 0 0
  1. public interface CustomDao {
  2.     List<Integer> getIds()
  3.     ModelObject getModelById(int id)
  4. }
  5.  
  6. class ModelObject {
  7.     int id
  8. }
  9.  
  10.  
  11. class CustomServiceUtil {
  12.     CustomDao customDao
  13.  
  14.     public List<Object> createOutputSet() {
  15.         List<ModelObject> models = new ArrayList<ModelObject>();
  16.         List<Integer> ids = customDao.getIds();
  17.         for (Integer id in ids) {
  18.             models.add(customDao.getModelById(id));
  19.         }
  20.         return models
  21.     }
  22. }
  23.  
  24. class TestSpec extends Specification {
  25.  
  26.     def 'crazy closures'() {
  27.         def mockDao = Mock(CustomDao)
  28.         def idSet = [9,10]
  29.  
  30.         given: 'An initialized object'
  31.         def customService = new CustomServiceUtil()
  32.         customService.customDao = mockDao
  33.  
  34.         when: 'createOutput is called'
  35.         def outputSet = customService.createOutputSet()
  36.  
  37.         then: 'the following methods should be called'
  38.         1*mockDao.getIds() >> {
  39.             return idSet
  40.         }
  41.  
  42.         idSet.each {
  43.             mockDao.getModelById(it) >> {int id ->
  44.                 def tmp = new ModelObject()
  45.                 println "Inside stubbed closure with value of id as $id"
  46.                 tmp.id = id
  47.                 return tmp
  48.             }
  49.         }
  50.  
  51.         and: 'each compute package is accurate'
  52.         2 == outputSet.size()
  53.         9 == outputSet.get(0).getId()
  54.         10 == outputSet.get(1).getId()
  55.     }
  56. }
  57.  
  58. //Prints
  59. Inside stubbed closure with value of id as 9
  60. Inside stubbed closure with value of id as 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement