Exception when using spock to test Java modulation

I am trying to use spock to test my Java based web application. We used to use Junit along with Mockito for testing.

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

But it does not work. I get 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 of the extension I am using for auto-tuning.

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

Below are my 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'

The following are the classes UserService and UserServiceImpl.

   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 an user entry with the user id: {}", userId);
        User user = userDAO.findById(userId); 
        userDAO.delete(user);
    }

}
+4
source share
2

, @Subject , .

UserService UserServiceImpl

+4

@Collaborator, @Subject

import com.blogspot.toomuchcoding.spock.subjcollabs.Collaborator
import com.blogspot.toomuchcoding.spock.subjcollabs.Subject

:

<dependency>
      <groupId>com.blogspot.toomuchcoding</groupId>
      <artifactId>spock-subjects-collaborators-extension</artifactId>
      <version>1.1.0</version>
      <scope>test</scope>
</dependency>

dependencies {
    testCompile 'com.blogspot.toomuchcoding:spock-subjects-collaborators-extension:1.1.0'
}
-1

Source: https://habr.com/ru/post/1626490/


All Articles