spockframework / spockframework/spock

Chained assignment of mock service fails to compile

Open
#283 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Module-Core Type-Defect
Dominant language
Java
Stars
3.6k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

Originally reported on Google Code with ID 161

In a Spec extending grails.plugin.spock.ControllerSpec, attempt to chain assignment
of a Mock() to the controller field and then a local variable.

eg
    AccountService accountService = controller.accountService = Mock()

This gives a Spock compilation failure.  The stack trace is below.

The equivalent statements
    AccountService accountService = Mock()
    controller.accountService = accountService
work correctly.

This is using the spock-0.5-groovy-1.7-SNAPSHOT plugin with the geb-0.4 plugin and
Grails 1.3.5, which uses groovy 1.7.5.

Stack trace:
Unexpected error during compilation of spec 'app.TransactionControllerSpec'. Maybe
you have used invalid Spock syntax? Anyway, please file a bug report at http://issues.spockframework.org.
java.lang.NullPointerException
    at org.spockframework.compiler.AstUtil.getType(AstUtil.java:275)
    at org.spockframework.compiler.AstUtil.expandBuiltinMemberAssignment(AstUtil.java:260)
    at org.spockframework.compiler.DeepStatementRewriter.visitBinaryExpression(DeepStatementRewriter.java:142)
    at org.spockframework.compiler.DeepStatementRewriter.visitDeclarationExpression(DeepStatementRewriter.java:135)
    at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:53)
    at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:69)
    at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:195)
    at org.spockframework.compiler.DeepStatementRewriter.visitExpressionStatement(DeepStatementRewriter.java:73)
    at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
    at org.spockframework.compiler.StatementReplacingVisitorSupport.replace(StatementReplacingVisitorSupport.java:44)
    at org.spockframework.compiler.StatementReplacingVisitorSupport.replaceAll(StatementReplacingVisitorSupport.java:59)
    at org.spockframework.compiler.DeepStatementRewriter.visitBlock(DeepStatementRewriter.java:60)
    at org.spockframework.compiler.SpecRewriter.visitAnyBlock(SpecRewriter.java:400)
    at org.spockframework.compiler.model.AnonymousBlock.accept(AnonymousBlock.java:32)
    at org.spockframework.compiler.model.Method.accept(Method.java:70)
    at org.spockframework.compiler.model.Spec.accept(Spec.java:102)
    at org.spockframework.compiler.SpockTransform$ActualTransform.processSpec(SpockTransform.java:76)
    at org.spockframework.compiler.SpockTransform$ActualTransform.visit(SpockTransform.java:63)
    at org.spockframework.compiler.SpockTransform.visit(SpockTransform.java:46)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:302)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:814)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:511)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:487)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:464)
    at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:43)
    at org.jetbrains.groovy.compiler.rt.GroovycRunner.main(GroovycRunner.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:75)

Reported by boocockp on 2010-12-17 06:43:01

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with AstUtil.getType and expandBuiltinMemberAssignment, then trace the declaration handling in DeepStatementRewriter.visitBinaryExpression and visitDeclarationExpression. Reproduce the chained assignment from the issue using the stated Spock and Groovy versions. Done means the declaration compiles successfully while the equivalent two statements continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
compilers, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.