Spock mock - 设置和获取参数
Spock mock - set and get parameter
我在项目中使用 atlassian 的 maven,想在 spock 框架中实现测试。我对来自 maven 的 Mock
classes 有疑问(例如 class Issue
- 它有一些私有值,如 key
或 summary
和没有像 setKey
和 setSummary
) 这样的方法。
我想要 Mock()
Issue
对象,设置一些值然后获取它(有 getKey()
和 getSummary()
)。
我的代码如下:
class WriterSpec extends Specification {
def returnValue() {
given:
Issue g = Mock(key: "exampleKey")
String response = g.getKey()
expect:
response == "exampleKey"
}
}
如果我尝试从 IDE 进行 运行 测试,则会出现错误(java class 中的 Issue
对象没有问题,就在 groovy):
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.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[=11=]0(InProcessGroovyc.java:51)
at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.call(InProcessGroovyc.java:85)
at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.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
):
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
我做了一些研究,但没有帮助。我不知道什么是问题以及为什么它不起作用。我应该怎么做才能解决它?
来自 pom 文件的@UPDATE 依赖项:
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-tests</artifactId>
<version>7.2.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>dom4j</artifactId>
<groupId>dom4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-api</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
<!-- spock-core -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<!-- Overrides the Groovy version specified by spock-core-->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.4</version>
<scope>test</scope>
</dependency>
<!-- Creates the test report -->
<dependency>
<groupId>com.athaydes</groupId>
<artifactId>spock-reports</artifactId>
<version>1.2.7</version>
<scope>test</scope>
</dependency>
</dependencies>
以下是仅测试 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}"
}
}
我在项目中使用 atlassian 的 maven,想在 spock 框架中实现测试。我对来自 maven 的 Mock
classes 有疑问(例如 class Issue
- 它有一些私有值,如 key
或 summary
和没有像 setKey
和 setSummary
) 这样的方法。
我想要 Mock()
Issue
对象,设置一些值然后获取它(有 getKey()
和 getSummary()
)。
我的代码如下:
class WriterSpec extends Specification {
def returnValue() {
given:
Issue g = Mock(key: "exampleKey")
String response = g.getKey()
expect:
response == "exampleKey"
}
}
如果我尝试从 IDE 进行 运行 测试,则会出现错误(java class 中的 Issue
对象没有问题,就在 groovy):
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.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[=11=]0(InProcessGroovyc.java:51)
at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.call(InProcessGroovyc.java:85)
at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.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
):
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
我做了一些研究,但没有帮助。我不知道什么是问题以及为什么它不起作用。我应该怎么做才能解决它?
来自 pom 文件的@UPDATE 依赖项:
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-tests</artifactId>
<version>7.2.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>dom4j</artifactId>
<groupId>dom4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-api</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
<!-- spock-core -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<!-- Overrides the Groovy version specified by spock-core-->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.4</version>
<scope>test</scope>
</dependency>
<!-- Creates the test report -->
<dependency>
<groupId>com.athaydes</groupId>
<artifactId>spock-reports</artifactId>
<version>1.2.7</version>
<scope>test</scope>
</dependency>
</dependencies>
以下是仅测试 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}"
}
}