-
Notifications
You must be signed in to change notification settings - Fork 40
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
Plugin causes exceptions when HibernateTestMixin is also used on a test #33
Conversation
…n the test Trying to mock domain classes when HibernateTestMixin was also used on a test results in an exception. This patch tries to detect HibernateTestMixin and stops the domain class mocking if it did.
We run into the same problem on try the "new" hibernate unit tests. Using grails 2.4.2 and hibernate4:4.3.5.4 I tried the pull request, but had the following problems.
Build-test-data is awesome and the bread and butter of our grails test based development! Any idea how to fix it? |
Thanks for the PR, from what @wuestenfuchs is saying, it sounds like this isn't quite fixing the error. The projects that I'm on right now are still using grails 2.3.X and hibernate 3 so I haven't gotten a chance to work with the new mixins yet. Do either of you have a simple test project that uses hibernate 4 and demonstrates the problem? I should probably add another test project to the root of the build-test-data repo (like the other projects I have there) that uses hibernate 4 as well as the hibernate annotation that you mention. From a quick look at the docs, it sounds like the want some sort of If you don't have a test case, I'll probably try to throw one together in the next week or so and take a closer look at grails 2.4/hibernate 4 |
I'll take a look at inherited tests, we just started a new client project so I haven't arrived at anything 'complex' yet (so I do have a simple test case, I am just not allowed to share it :) ). But a word of warning, @wuestenfuchs: I had to clean the entire Grails project whenever I made any changes whatsoever in |
Here's a quick test case for @tednaleid. The Now I'll try creating test classes that extend each other and see where that goes. |
I tried detecting if I guess I am trying to say I would appreciate a test after the new commit, but please |
Hmm, on the other hand, don't bother: I can still make it fail, it was just too early in the morning to see that before that last comment :) @tednaleid: the test case branch now contains a EDIT: huh, now my tests fail even if I remove |
Grails bug reported. (hmm, JIRA isn't exactly Spock-friendly :P) |
Hello. Thank you very much for reporting the grails bug. I wasn't sure on that. I experienced the same thing as you, randomly strange behavior, unable to identify. I spent 1 days developing with rm -r target && grails... trying to isolate something. All our domains, around 100, all related to each other, and @Domain requires all related domains to be mocked as well, so it took some time to get it to compile. You are right, in this case I didn't even try a clean, sorry for that. changes to BuildTransformation.java really require each time a clean. Now (doGrailsMocking = false) I'm having different issue: Without @build(User) everything runs, User.count works. If I add @build(User) (without calling User.build() yet)I get on User.count: java.lang.IllegalStateException: Method on class [com.chat.authentication.User] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly. without any other useful information. |
Huh, that seems like a different problem, maybe you can open another issue? One difference that I see is that you seem to be using JUnit tests while I'm working with Spock.. I can try making a JUnit test to see what happens but it definitely looks like a separate issue. Perhaps Grails isn't always picking up the Can you try using both |
Yes seems you are right. If I replace @Domain and @TestMixin(HibernateTestMixin) with @mock it works. There are strange things going on, I just reported https://jira.grails.org/browse/GRAILS-11575 produces the same java.lang.IllegalStateException, just without any use of build-test-data plugin. |
Following along with the stuff you guys tried, it seems like you both concluded that these are grails bugs and not build-test-data bugs. Right? Should we close this issue (and reopen if it does end up being a BTD thing)? If not, let me know what testcase I should be using to repeat what you guys are seeing with BTD. |
Well the original reason I wrote this patch is not a Grails bug by itself: by using both What my patch does is disable As for the test case, you can clone the repository I posted a few comments above and The inherited test/spec class problems are indeed a Grails bug. |
yes, I agree. Getting the plugin to work together with @TestMixin(HibernateTestMixin) I would consider very helpful. |
@gregopet got it thanks for the explanation. I wasn't previously understanding that patching in the equivalent of Thanks for the patch and the sample test project. I'm taking a closer look at it this weekend and want to have some tests in the build-test-data project that exercise the functionality, so I'll probably upgrade the whole thing to 2.4 to get that working. |
I've got a working test locally in build-test-data in a new test project called I'm not releasing it yet though as I want to make a few changes to it so that the I need to do some more investigation, but my hope is that if we detect that the user is using the Thanks again for the pull request, and your work to dig into what's going on with the new hibernate annotations in 2.4. |
Plugin causes exceptions when HibernateTestMixin is also used on a test
Looks like the It doesn't create I've opened up GRAILS-11579 to report the issue. When they get that fixed, I should be able to make BTD work with what you've done, but until that's in the plugin is pretty much useless in |
Glad to have been of help! I know about the |
@gregopet @tednaleid Besides HibernateTestMixin, the next version of grails-datastore-test-support will also include a similar feature for using real Mongo DB in unit tests. Please see https://jira.grails.org/browse/GRAILS-11533 for details of MongoDbTestMixin . As you have probably noticed, the unit test runtime API (including the TestMixin) in Grails 2.4 has been heavily refactored as part of https://jira.grails.org/browse/GRAILS-11001 . There is some explanation in the "What's new in 2.4" guide http://grails.org/doc/latest/guide/introduction.html#whatsNew24 under "Unit Testing improvements". The static fields for keeping state in TestMixin classes has been removed and all state is kept in a separate TestRuntime instance. Because of this change we had to move HibernateTestMixin and MongoDbTestMixin classes to the https://github.com/grails/grails-datastore-test-support module. The master branch currently has support for Grails 2.4.x and the 2.3.x branch of this module is for Grails 2.3.x. . (I hope you don't have to do something similar for build-test-data) . build-test-data is a very useful plugin. I'd like to help you in getting build-test-data compatible with HibernateTestMixin and MongoDbTestMixin . You can ping me directly on Skype , my nick is lhotari there. |
@lhotari thanks for the links and the work you did to fix the defect we uncovered (as well as all the other unit test improvements that you've done)! That's interesting info about the I think you're going to be at gr8conf.us in a week or so. If you have time, we should talk there about your plans for the grails-datastore-test-support and whether you plan on adding a lot more mixins. Maybe there's a better way that I can be detecting that I should be applying the The alternative is to not have my annotation be that smart and instead have an alternate I'm going to open up another issue (as this one is marked as closed) to track the ideas/work on getting this implemented. It'll be linked here shortly. |
What I've decided to do is that instead of looking for It will also make it easier in the future to split out Build Test Data from grails and make it support stand-alone apps that just use GORM (like Ratpack apps). That's a longer term goal, but something I'm thinking about. This fix will be released shortly. Thanks again for the patch, sorry it took so long to release. |
This is (a probably naive) attempt to enable using both the build-test-data plugin as well as the new HibernateTestMixin mixin for Grails.
I love the plugin's functionality but some tests do not work properly with Grails' mocked ORM (a named query in my case). That's why I tried using the new HibernateTestMixin annotation (at least it is new to me..) and my test was now working - but the ones using the
@Build
annotation suddenly failed with the following exception under Grails 2.4.2:The patch is my attempt at solving this issue (though I'm not intimately familiar with Grails' internals so a better solution may well exist).