【问题标题】:Exception while using spock for java unit testing使用 spock 进行 java 单元测试时出现异常
【发布时间】:2016-05-06 20:30:40
【问题描述】:

我正在尝试使用 spock 来测试我的基于 java 的 Web 应用程序。之前我们曾经使用 Junit 和 Mockito 进行测试。

我正在尝试使用@Collaborator 和@Subject 来自动装配我的依赖项,就像我们过去在Mockito 中使用@Mock 和@InjectMocks 一样。

但它不起作用。我收到以下错误。

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) 

下面是我的测试代码。

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
    }
}

下面是我用于自动装配的扩展的 URL。

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

以下是我的依赖项。

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'

下面是我的 UserService 和 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);
    }

}

【问题讨论】:

  • 您使用的 spock 版本是否大于或等于 1.0.2?如果没有,UserService 是否有公共构造函数?
  • 我使用的spock版本是org.spockframework:spock-core:1.0-groovy-2.4
  • 嗨!您也可以发布 UserDAO 和 SecurityUtil 代码吗?也许您可以在github.com/marcingrzejszczak/… 发布示例项目并提交问题,我们可以在 gitter -gitter.im/marcingrzejszczak/… 继续讨论?
  • 哦,顺便说一句,如果你通过构造函数自动装配,你就不必做所有这些魔法了 :)
  • 如果有很多依赖怎么办?

标签: java unit-testing spock


【解决方案1】:

问题与@Subject 注释类应该是实现而不是接口有关。

UserService改成UserServiceImpl就够了

【讨论】:

    【解决方案2】:

    @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'
    }
    

    【讨论】:

    • 我的项目中已经有了这个依赖项。此外,注释也被导入。
    • 如果问题出在 import 语句上,代码将无法编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2013-01-21
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多