Exception while using spock for java unit testing -


i trying use spock testing java based web application. earlier used use, junit along mockito testing.

i trying use @collaborator , @subject autowire dependencies used use @mock , @injectmocks in mockito.

but not working. getting following error.

java.lang.nullpointerexception     @ com.blogspot.toomuchcoding.spock.subjcollabs.nonconstructorbasedinjector.instantiatesubjectandsetonspecification(nonconstructorbasedinjector.groovy:22)     @ com.blogspot.toomuchcoding.spock.subjcollabs.propertyinjector.trytoinject(propertyinjector.groovy:16)     @ com.blogspot.toomuchcoding.spock.subjcollabs.subjectscollaboratorsinterceptor.trytoinjectcandidatesintosubject_closure2(subjectscollaboratorsinterceptor.groovy:74)     @ com.blogspot.toomuchcoding.spock.subjcollabs.subjectscollaboratorsinterceptor.trytoinjectcandidatesintosubject(subjectscollaboratorsinterceptor.groovy:74)     @ com.blogspot.toomuchcoding.spock.subjcollabs.subjectscollaboratorsinterceptor.intercept_closure1(subjectscollaboratorsinterceptor.groovy:69)     @ groovy.lang.closure.call(closure.java:426)     @ groovy.lang.closure.call(closure.java:442)     @ com.blogspot.toomuchcoding.spock.subjcollabs.subjectscollaboratorsinterceptor.intercept(subjectscollaboratorsinterceptor.groovy:69)     @ org.spockframework.runtime.extension.methodinvocation.proceed(methodinvocation.java:87)     @ org.junit.runner.junitcore.run(junitcore.java:137)     @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117)     @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:234)     @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:74)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:144)  

below test code.

class userserviceimplspec extends specification {      @collaborator     userdao userdao = mock()      @subject     userservice userservice      def "should delete user"(){         given: "given user delete"             user usertodelete = make(a(usermaker.user));         when:             userservice.deleteuser(usertodelete.getid());         then:             true     } } 

below url extension using autowiring.

https://github.com/marcingrzejszczak/spock-subjects-collaborators-extension

below dependencies.

testcompile 'org.codehaus.groovy:groovy-all:2.4.5' testcompile 'org.spockframework:spock-core:1.0-groovy-2.4' testcompile 'org.spockframework:spock-spring:1.0-groovy-2.4' testcompile "cglib:cglib:2.2" testcompile 'org.objenesis:objenesis:2.2' testcompile 'com.blogspot.toomuchcoding:spock-subjects-collaborators-extension:1.1.0' 

below userservice , userserviceimpl classes.

   public interface userservice {         public void deleteuser(long userid);     }   @service("userservice") @transactional(readonly = true) public class userserviceimpl implements userservice {      @autowired     private userdao userdao;      @autowired     private securityutil securityutil;      @override     @transactional(propagation = propagation.required, readonly = false, rollbackforclassname = {"java.lang.exception" })     public void deleteuser(long userid) {         log.debug("deleting user entry user id: {}", userid);         user user = userdao.findbyid(userid);          userdao.delete(user);     }  } 

the problem related fact @subject annotated class should implementation , not interface.

it's enough change userservice userserviceimpl


Comments