Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GroovyCastException when invoking TestFor generated getters in tests #10363

Closed
4 tasks done
dpcasady opened this issue Dec 17, 2016 · 4 comments
Closed
4 tasks done

GroovyCastException when invoking TestFor generated getters in tests #10363

dpcasady opened this issue Dec 17, 2016 · 4 comments
Assignees
Milestone

Comments

@dpcasady
Copy link
Contributor

dpcasady commented Dec 17, 2016

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create a Grails artefact (domain, controller, or service) with a property of name, or a getName() method.
  2. Create a test that references the artefact as the class under test with the @TestFor annotation.
  3. Invoke the generated getter for the artefact in the test (getDomain(), getController(), getService()).

Expected Behaviour

For artefacts that don't have a name property there are no problems and an instance of the artefact is returned by the getter. I would expect the same to hold true if there is a name property.

Actual Behaviour

Instead, the getter throws a GroovyCastException. For a domain class, Bar:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class foo.Bar' with class 'java.lang.Class' to class 'foo.Bar'
	at foo.BarSpec.class under test getter throws exception(BarSpec.groovy:11)

From what I can tell, the exception is thrown in the getter method in the process of checking if the bean exists in the application context. I've tried decompiling the compiled test and for an artefact without a name property, the AST generated code looks something like this:

applicationContext.containsBean(Bar.class.getName())

For an artefact with the name property, the AST generated code looks like this:

applicationContext.containsBean(((Bar)ScriptBytecodeAdapter.castToType((Object)Bar.class, (Class)Bar.class)).getName())

This seems to be the cast that is throwing the exception. The relevant source code seems to be in TestForTransformation, but I'm honestly not sure if this is actually more of an issue with the Groovy AST transform api itself.

Environment Information

  • Operating System: macOS Sierra 10.12.1
  • Grails Version: 3.2.3
  • JDK Version: 1.8.0_101

Example Application

This bug report has three test classes (one for each kind of artefact). The tests pass if an exception is thrown when invoking the getter.
foo-bug-report-17122016.zip

@osscontributor
Copy link
Member

The .zip file linked above is not runnable in its current state.

Would you test the project at https://github.com/jeffbrown/issue10363 and let me know if the problem occurs for you there? I can't reproduce the problem.

Thanks for the feedback.

@dpcasady
Copy link
Contributor Author

Sorry about that .zip file. Yeah it still happens on that project.

Are the tests failing for you? Maybe it's a bit counter-intuitive, but they should pass if the exception is thrown:

void "class under test getter throws exception"() {
    when:
    getDomain()

    then:
    thrown(ClassCastException) // test should pass if the bug is present
}

@osscontributor
Copy link
Member

Maybe it's a bit counter-intuitive, but they should pass if the exception is thrown

Ok. I was looking for failing tests. I see now.

Thanks for the feedback.

@osscontributor
Copy link
Member

See osscontributor/issue10363@d71e024. That makes more sense to me as the tests now describe how the system is supposed to behave.

osscontributor pushed a commit to grails/grails-testing-support that referenced this issue Dec 20, 2016
@osscontributor osscontributor added this to the grails-3.0.18 milestone Dec 20, 2016
@osscontributor osscontributor self-assigned this Dec 20, 2016
osscontributor pushed a commit that referenced this issue Dec 20, 2016
@osscontributor osscontributor modified the milestones: grails-3.1.15, grails-3.0.18 Dec 20, 2016
graemerocher pushed a commit to grails-plugins/grails-test-mixin-plugin that referenced this issue Jun 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants