jeudi 28 janvier 2016

Exception while using spock for java unit testing

I am trying to use spock for testing my java based web application. Earlier we used to use, Junit along with Mockito for testing.

I am trying to use @Collaborator and @Subject to autowire my dependencies like we used to use @Mock and @InjectMocks in Mockito.

But it is not working. I am getting the following error.

java.lang.NullPointerException
    at com.blogspot.toomuchcoding.spock.subjcollabs.NonConstructorBasedInjector.instantiateSubjectAndSetOnSpecification(NonConstructorBasedInjector.groovy:22)
    at com.blogspot.toomuchcoding.spock.subjcollabs.PropertyInjector.tryToInject(PropertyInjector.groovy:16)
    at com.blogspot.toomuchcoding.spock.subjcollabs.SubjectsCollaboratorsInterceptor.tryToInjectCandidatesIntoSubject_closure2(SubjectsCollaboratorsInterceptor.groovy:74)
    at com.blogspot.toomuchcoding.spock.subjcollabs.SubjectsCollaboratorsInterceptor.tryToInjectCandidatesIntoSubject(SubjectsCollaboratorsInterceptor.groovy:74)
    at com.blogspot.toomuchcoding.spock.subjcollabs.SubjectsCollaboratorsInterceptor.intercept_closure1(SubjectsCollaboratorsInterceptor.groovy:69)
    at groovy.lang.Closure.call(Closure.java:426)
    at groovy.lang.Closure.call(Closure.java:442)
    at com.blogspot.toomuchcoding.spock.subjcollabs.SubjectsCollaboratorsInterceptor.intercept(SubjectsCollaboratorsInterceptor.groovy:69)
    at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

Below is my test code.

class UserServiceImplSpec extends Specification {

    @Collaborator
    UserDAO userDAO = Mock()

    @Subject
    UserService userService

    def "should delete a user"(){
        given: "given a user to delete"
            User userToDelete = make(a(UserMaker.User));
        when:
            userService.deleteUser(userToDelete.getId());
        then:
            true
    }
}

Below is the URL for the extension I am using for autowiring.

http://ift.tt/1arXXxL

Aucun commentaire:

Enregistrer un commentaire