Próbuję zrobić proste kpiny z klasy Domain w ramach testu jednostkowego dla usługi Grails zgodnie z dokumentacją, ale nie wydaje się działać w ogóle, w zapytaniach dla instancji zawsze return null. Czy brakuje mi czegoś prostego? Tutaj jest odpowiednia część mojego kodu, mam zmieniony klasy i metody testowania nazwy dla jasności:Połączenie MockDomain zapewniające dane domen urządzeń nie działa, czego mi brakuje? Grails 2.0.1
@TestFor(MyService)
@TestMixin(DomainClassUnitTestMixin)
class MyServiceTests {
void testMyThing() {
defineBeans {anotherService(AnotherService)} //My service under test uses another service, unlikely relevant?
MyUser.metaClass.isDirty = { //workaround for mockDomain not adding isDirty method.
println("dirty check called");
}
mockDomain(MyUser, [
[username: "[email protected]", accountType: UserType.STANDARD, id: 1L],
[username: "[email protected]", accountType: UserType.STANDARD, id:3L],
[username: "[email protected]", accountType: UserType.BUSINESS, id:2L]
])
MyUser user1 = MyUser.get(1);
System.out.println("user 1: ${user1}"); // output is 'user 1: null'
MyUser user1byName = MyUser.findByUsername("[email protected]");
System.out.println("user 1 by name: ${user1byName}"); // output is 'user 1 by name: null'
... the actual testing stuff which would love to have non null MyUser objects ...
}
}
Czy próbowałeś dodać @Mock (MyUser)? –
@tomy Tak, zrobiłem, bez efektu. Biorąc pod uwagę dokumentację, nie powinno być potrzebne przy używaniu mockDomain() – Peter