I have a very weird error, hope you can help me! I'm currently using grails 2.5.1.
My service is something like this:
@Transactional
abstract class MyParentService {
AnotherService anotherService
[...]
@Transactional(readOnly = true)
List<Project> allProjects(){
anotherService.doStuff()
}
[...]
}
Same result if service is not abstract. Then, I have another service that extends that one:
@Transactional
class MyChildService extends MyParentService {
def aMethod(){
List<Projects> projects = allProjects()
[...]
}
}
Then I have a spock unit test:
@TestFor(MyChildService)
class MyChildServiceSpec extends Specification {
AnotherService anotherService
def setup(){
anotherService = Mock()
service.anotherService = anotherService
List<Project> list = [new Project(a: 1), new Project(a:2)]
anotherService.doStuff() >> list
}
void "do some stuff"(){
when:
aMethod()
then:
//some asserts
}
}
When I run the tests, I get an NPE error in this line:
List<Projects> projects = allProjects()
The method allProjects is never called (i added some traces and never are shown).
The weird thing is that if I comment "= allProjects()" out, execute the test (of course, it fails because of asserts), remove the comment again and execute test again, it passes. But later on, it will fail again for same NPE reason.
I haven't got the error when executing the code using the application, so it's only a unit test thing
Any ideas?
Aucun commentaire:
Enregistrer un commentaire