ajm
ajm

Reputation: 13213

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.

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'

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

}

Upvotes: 4

Views: 995

Answers (2)

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11149

The problem was related to the fact that @Subject annotated class should be an implementation and not an interface.

It's enough to change UserService into UserServiceImpl

Upvotes: 4

you have a problem with inject annotations @Collaborator, @Subject

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

Add dependency:

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

OR

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

Upvotes: -1

Related Questions