Spock mock - 设置并获取参数

时间:2017-09-28 08:54:52

标签: java maven unit-testing groovy spock

我在我的项目中使用atlassian的maven并希望在spock框架中实现测试。我遇到来自maven的Mock个类的问题(例如,类Issue - 它有一些私有值,如keysummary,并且没有像{{1}这样的方法}和setKey)。

我想要setSummary Mock()个对象,设置一些值然后得到它(有IssuegetKey())。

我的代码如下:

getSummary()

如果我尝试从IDE运行测试,则会出现错误(java类中的class WriterSpec extends Specification { def returnValue() { given: Issue g = Mock(key: "exampleKey") String response = g.getKey() expect: response == "exampleKey" } } 对象没有问题,只是在groovy中):

Issue

如果我尝试从控制台运行测试(使用Information:Groovyc: While compiling tests of exampleProject:java.lang.NoClassDefFoundError: Unable to load class com.atlassian.jira.issue.Issue due to missing dependency org/ofbiz/core/entity/GenericValue at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:391) at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:265) at org.codehaus.groovy.ast.ClassNode.getDeclaredMethods(ClassNode.java:872) at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:887) at org.spockframework.compiler.SpecialMethodCall.checkIsConditionBlock(SpecialMethodCall.java:194) at org.spockframework.compiler.SpecialMethodCall.parse(SpecialMethodCall.java:151) at org.spockframework.compiler.AbstractDeepBlockRewriter.visitMethodCallExpression(AbstractDeepBlockRewriter.java:110) at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:66) at org.codehaus.groovy.ast.CodeVisitorSupport.visitBinaryExpression(CodeVisitorSupport.java:146) at org.spockframework.compiler.AbstractDeepBlockRewriter.doVisitBinaryExpression(AbstractDeepBlockRewriter.java:150) at org.spockframework.compiler.AbstractDeepBlockRewriter.visitBinaryExpression(AbstractDeepBlockRewriter.java:98) at org.spockframework.compiler.DeepBlockRewriter.visitDeclarationExpression(DeepBlockRewriter.java:86) at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89) at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:71) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:196) at org.spockframework.compiler.AbstractDeepBlockRewriter.doVisitExpressionStatement(AbstractDeepBlockRewriter.java:146) at org.spockframework.compiler.DeepBlockRewriter.doVisitExpressionStatement(DeepBlockRewriter.java:61) at org.spockframework.compiler.AbstractDeepBlockRewriter.visitExpressionStatement(AbstractDeepBlockRewriter.java:87) at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42) at org.spockframework.compiler.StatementReplacingVisitorSupport.replace(StatementReplacingVisitorSupport.java:44) at org.spockframework.compiler.AbstractDeepBlockRewriter.visit(AbstractDeepBlockRewriter.java:71) at org.spockframework.compiler.DeepBlockRewriter.visit(DeepBlockRewriter.java:49) at org.spockframework.compiler.SpecRewriter.visitAnyBlock(SpecRewriter.java:311) at org.spockframework.compiler.model.SetupBlock.accept(SetupBlock.java:32) at org.spockframework.compiler.model.Method.accept(Method.java:70) at org.spockframework.compiler.model.Spec.accept(Spec.java:113) at org.spockframework.compiler.SpockTransform$Impl.processSpec(SpockTransform.java:77) at org.spockframework.compiler.SpockTransform$Impl.visit(SpockTransform.java:64) at org.spockframework.compiler.SpockTransform.visit(SpockTransform.java:47) at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:321) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546) at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:62) at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:115) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:136) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:156) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.access$000(InProcessGroovyc.java:51) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:85) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:82) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) ):

atlas-mvn clean test

我做了一些研究,但没有帮助。我不知道什么是问题,为什么它不起作用。我该怎么做才能解决它?

来自pom文件的

@UPDATE依赖:

Running example.WriterSpec
wrz 28, 2017 10:51:35 AM org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrap invoke
INFO: Configuring com.athaydes.spockframework.report.SpockReportExtension
wrz 28, 2017 10:51:35 AM org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrap invoke
INFO: SpockReports config loaded: [com.athaydes.spockframework.report.template.TemplateReportCreator.reportFileExtension:md, com.athayde
s.spockframework.report.template.TemplateReportCreator.specTemplateFile:/templateReportCreator/spec-template.md, com.athaydes.spockframe
work.report.IReportCreator:com.athaydes.spockframework.report.internal.HtmlReportCreator, com.athaydes.spockframework.report.template.Te
mplateReportCreator.summaryTemplateFile:/templateReportCreator/summary-template.md, com.athaydes.spockframework.report.internal.HtmlRepo
rtCreator.summaryReportCss:spock-summary-report.css, com.athaydes.spockframework.report.template.TemplateReportCreator.summaryFileName:s
ummary.md, com.athaydes.spockframework.report.internal.HtmlReportCreator.featureReportCss:spock-feature-report.css, com.athaydes.spockfr
amework.report.hideEmptyBlocks:false, com.athaydes.spockframework.report.outputDir:build/spock-reports]
wrz 28, 2017 10:51:35 AM org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrap invoke
INFO: Loading settings for reportCreator of type com.athaydes.spockframework.report.internal.HtmlReportCreator
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.718 sec <<< FAILURE!
returnValue(example.WriterSpec)  Time elapsed: 0.05 sec  <<< ERROR!
java.lang.NoClassDefFoundError: org/apache/lucene/search/FieldComparatorSource
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetPublicMethods(Class.java:2902)
        at java.lang.Class.privateGetPublicMethods(Class.java:2911)
        at java.lang.Class.getMethods(Class.java:1615)
        at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
        at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
        at example.WriterSpec.returnValue(WriterSpec.groovy:13)
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.search.FieldComparatorSource
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

1 个答案:

答案 0 :(得分:3)

以下是仅测试returnIssueKeyWithPrefix的方法:

import spock.lang.Specification

class LolSpec extends Specification {

    def 'whatever'() {
        given:
        def prefixer = new Prefixer()
        def issue = Mock(Issue) {
            getKey() >> '1234'
        }

        when:
        def withPrefix = prefixer.returnIssueKeyWithPrefix(issue)

        then:
        withPrefix == 'prefix1234'
    }
}

class Issue {
    String getKey() {
        throw new UnsupportedOperationException('No!')
    }
}

class Prefixer {
    String returnIssueKeyWithPrefix(Issue issue) {
        return "prefix${issue.key}"
    }
}