What is causing "exception in phase 'instruction selection' " in Groovy / Grails unit test?
Asked Answered
P

2

7

Occasionally, when making some updates to a Grails unit test, I will run into an error along the following lines:

BUG! exception in phase 'instruction selection' in source unit ... unexpected NullpointerException

What causes this? Or what is the best strategy to debug this problem?

Plausive answered 21/9, 2015 at 21:14 Comment(1)
What version of Grails? These BUG! errors should usually be reported to the Groovy team when encountered (assuming you can make a really small test case that exhibits the problem)Canvass
P
8

My best approach thus far has been to iteratively comment things out until the code compiles. A grails clean does not fix the issue.

Using this process I've found that the problem is my @TestFor(Class) annotation. I had moved some code from a service to a class in src/groovy. This appears to have caused the annotation to break. I don't really understand why specifically this breaks, but that was sufficient for me to get things working.

So try removing the @TestFor() annotation if your class under test is not a service or controller or similar.

If that doesn't work, comment everything out and add back one piece at a time.

Plausive answered 21/9, 2015 at 21:14 Comment(3)
Unbelievable that this is still an issue in 2017.Winkler
@Winkler I'm think that is a completely unfair comment. For one thing, I haven't encountered this issue since I answered the question. Second, the issue was really a mistake in my code, but the error I received simply didn't point me directly to that. As I mentioned in my answer, the @TestFor() annotation doesn't make sense "if your class under test is not a service or controller or similar." Not a bug in grails or groovy that hasn't been resolved.Plausive
I encountered the same issue from controller test code generated by Grails - it doesn't compile until you comment out @TestFor(NameOfController.class).Winkler
P
0

In my case my service class don't have default constructor. I resolved this issue by providing default constructer As, @TestFor wants to inject service bean, as my service bean don't have default constructor spring container unable to inject.

Porta answered 17/1, 2020 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.