-
Notifications
You must be signed in to change notification settings - Fork 395
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
Cannot cast object to an interface type during test #679
Comments
On first glance, that seems like a really weird error. I'm not exactly sure why that is happening here. My second question would be, since you have gone to the bother of having a properly structured pipeline library, why you would bother testing it by loading the library and then running I realize of course that this is a simplified example and your use case may be much different (and you also may not be able to share the code for said use case), but I'll just say that at my company we don't use JenkinsPipelineUnit to test such things, either complete Jenkinsfiles or the singletons. We've intentionally put all testable logic in classes and only test those classes; the singleton functions are (with very few exceptions) simple passthru methods that don't necessitate unit tests. |
Thanks for the response. You are right, actual code that I have problem with is bit more complex and property of the company I work for, so I had to prepare an example of a problem that is easy to understand and replicate. To answer your question about the testing part - I'm of opinion that it's best to use the same API for testing as the one that will be used by its users. In the case of the shared library it would be mostly in We were able to write quite a number of tests that way with implementations that are ranging between usage of To sum up: I'm not really able to change the approach to testing right now, and I would like to be able to use inheritance and interface in the implementations. That means I will have to figure it out myself. I think this problem has something to do with custom implementation of GroovyClassLoader that is in the JenkinsPipelineUnit, because if I write a script that just load those classes using default GCL then calling them works just fine. Or maybe there is something else going on later during execution. I will let you know if I arrive at any conclusions. (Sorry for late response, I was on a vacation) |
Ok, I was wrong about the custom implementation of GroovyClassLoader. I found out that the problem disappears if I turn off library class preloading. However, this is not an acceptable solution - in my actual code I depend on being able to access Jenkins variables and steps all over the place - which stops working immediately when I disable preloading. Any idea how this code in the LibraryLoader.groovy
could be causing problems? |
Hello everyone. I was using this framework to create unit tests for newly created Jenkins shared library. I was pretty happy with this until I've encountered a problem that is outlined below - I would greatly appreciate any help as I'm currently at my wits ends.
High level description:
I'm experiencing a problem during test execution: I'm trying to create an instance of an object and then cast it to the interface type that this class implements. I'm getting GroovyCastException. The same code works correctly on Jenkins.
Details:
Environment:
I've distilled the problem to the smallest reproducible setup.
Let's assume following project structure:
AFoo.groovy:
IFoo.groovy:
helloWorld.groovy:
I'm loading the current library during the tests and trying to execute helloWorld step in HelloWorldTest.groovy:
My build.gradle:
If I run
gradle :test
I'm getting following error:I've tried many things - none of which worked: using normal inheritance (instead of using interfaces), moving interface to a different package, trying few different versions of groovy or this library (in a few combinations), and few others that I can't recall right now.
Conversely this happens on groovy 2.*, on Groovy 3.0.13 which I initially used to compile and run tests with it doesn't error - it hangs on parseClass in GroovyClassLoader.
I would really appreciate any help - either in solving this error outlined above or in getting this to run with Groovy 3.0.13 (which I would prefer but I know that you have no reason to support that).
Thank you in advance.
The text was updated successfully, but these errors were encountered: