Guest User

Untitled

a guest
Jul 30th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. MockDomain call providing fixture domain data not working, what am I missing? Grails 2.0.1
  2. @TestFor(MyService)
  3. @TestMixin(DomainClassUnitTestMixin)
  4.  
  5. class MyServiceTests {
  6.  
  7.  
  8. void testMyThing() {
  9. defineBeans {anotherService(AnotherService)} //My service under test uses another service, unlikely relevant?
  10.  
  11. MyUser.metaClass.isDirty = { //workaround for mockDomain not adding isDirty method.
  12. println("dirty check called");
  13. }
  14.  
  15. mockDomain(MyUser, [
  16. [username: "email@gmail.com", accountType: UserType.STANDARD, id: 1L],
  17. [username: "user@gmail.com", accountType: UserType.STANDARD, id:3L],
  18. [username: "bizuser@domain.com", accountType: UserType.BUSINESS, id:2L]
  19. ])
  20.  
  21. MyUser user1 = MyUser.get(1);
  22. System.out.println("user 1: ${user1}"); // output is 'user 1: null'
  23.  
  24. MyUser user1byName = MyUser.findByUsername("email@gmail.com");
  25. System.out.println("user 1 by name: ${user1byName}"); // output is 'user 1 by name: null'
  26.  
  27. ... the actual testing stuff which would love to have non null MyUser objects ...
  28. }
  29. }
  30.  
  31. @Mock(MyUser)
  32. class MyServiceTests {
  33. ...
  34.  
  35. void testMyThing() {
  36. new MyUser(username: "email@gmail.com",
  37. accountType: UserType.STANDARD,
  38. id: 1L).save(failOnError:true) //throws exception because MyUser requires password field to be non blank
  39. ...
  40. }
  41. }
Add Comment
Please, Sign In to add comment