gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:45 25 4
gpt4 key购买 nike

我正在尝试使用 spock 来测试我基于 Java 的 Web 应用程序。早些时候我们曾经使用 Junit 和 Mockito 进行测试。

我正在尝试使用@Collaborator 和@Subject 来 Autowiring 我的依赖项,就像我们过去在 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
}
}

下面是我用于 Autowiring 的扩展程序的 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);
}

}

最佳答案

问题与@Subject 注释的类应该是实现而不是接口(interface)这一事实有关。

UserService改成UserServiceImpl就可以了

关于java - 使用 spock 进行 java 单元测试时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com