Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用spock进行java单元测试时出现异常_Java_Unit Testing_Spock - Fatal编程技术网

使用spock进行java单元测试时出现异常

使用spock进行java单元测试时出现异常,java,unit-testing,spock,Java,Unit Testing,Spock,我正在尝试使用spock测试基于java的web应用程序。之前我们使用Junit和Mockito进行测试 我正在尝试使用@Collaborator和@Subject来自动连接我的依赖项,就像我们在Mockito中使用@Mock和@injectmock一样 但它不起作用。我得到以下错误 java.lang.NullPointerException at com.blogspot.toomuchcoding.spock.subjcollabs.NonConstructorBasedInjec

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

我正在尝试使用@Collaborator和@Subject来自动连接我的依赖项,就像我们在Mockito中使用@Mock和@injectmock一样

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

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

下面是我的依赖项

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

}

您在@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>

问题在于@Subject注释类应该是一个实现,而不是一个接口


UserService
更改为
userserviceinpl

就足够了。您是否使用大于或等于1.0.2的spock版本?如果没有,那么
UserService
是否有公共构造函数?我正在使用的spock版本是org.spockframework:spock core:1.0-groovy-2.4hi!您是否也可以发布UserDAO和SecurityUtil代码?也许你可以发布一个示例项目并在这里提交一个问题,我们可以在gitter继续讨论-?哦,顺便说一句,如果你通过构造函数自动连线,你就不必做所有这些魔术:)如果有很多依赖项呢?我的项目中已经有了这些依赖项。此外,注释也被导入。如果问题出在导入语句上,代码将无法编译。
dependencies {
    testCompile 'com.blogspot.toomuchcoding:spock-subjects-collaborators-extension:1.1.0'
}