【发布时间】: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