Custom test mixins or reusing test code?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom test mixins or reusing test code?

johnr
This is another I-just-upgraded-to-grails-2.x story.  I want to reuse
unit test setup code but keep hitting snags.  It seems like I should
be able to do something like...

@TestMixin(CustomTestMixin)
class MyTests {
  ...
}

@Mock([Book, Author])
class CustomTestMixin extends GrailsUnitTestMixin {
  ...
}

This doesn't work, of course.  I get errors like
MissingMethodException for Book.get() and various NPEs in
GrailsUnitTestMixin and DomainClassUnitTestMixin.  The latter seem to
be caused by @BeforeClass methods not executing in the right order.  I
found a hack in someone's post about calling
DomainClassUnitTestMixin.initializeDatastoreImplementation() directly,
which fixed most of the NPEs.  Moving the @Mock annotation from
CustomTestMixin to MyTests doesn't change anything.

What's the recommended way to reuse setup/teardown code in 2.2 unit
tests?  Specifically, I'd like to

* mock a few (4-6) domain classes
* set up a small self-referential set of data
* use domain class get() methods, but not the findByXXX methods
* run the tests in GGTS and avoid the startup/shutdown overhead of
integration tests

I'd rather not rewrite all my tests for spock, but am I out of luck
with the standard grails test framework?

Thanks,

-John

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Custom test mixins or reusing test code?

sergiomichels
The class calls initGrailsApplication(), maybe you need the same flow?

// this has to be called first as there's no declarative way to enforce
// execution order
initGrailsApplication()


--
Sérgio Michels


On Tue, May 14, 2013 at 1:56 PM, John Reuning <[hidden email]> wrote:
This is another I-just-upgraded-to-grails-2.x story.  I want to reuse
unit test setup code but keep hitting snags.  It seems like I should
be able to do something like...

@TestMixin(CustomTestMixin)
class MyTests {
  ...
}

@Mock([Book, Author])
class CustomTestMixin extends GrailsUnitTestMixin {
  ...
}

This doesn't work, of course.  I get errors like
MissingMethodException for Book.get() and various NPEs in
GrailsUnitTestMixin and DomainClassUnitTestMixin.  The latter seem to
be caused by @BeforeClass methods not executing in the right order.  I
found a hack in someone's post about calling
DomainClassUnitTestMixin.initializeDatastoreImplementation() directly,
which fixed most of the NPEs.  Moving the @Mock annotation from
CustomTestMixin to MyTests doesn't change anything.

What's the recommended way to reuse setup/teardown code in 2.2 unit
tests?  Specifically, I'd like to

* mock a few (4-6) domain classes
* set up a small self-referential set of data
* use domain class get() methods, but not the findByXXX methods
* run the tests in GGTS and avoid the startup/shutdown overhead of
integration tests

I'd rather not rewrite all my tests for spock, but am I out of luck
with the standard grails test framework?

Thanks,

-John

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Custom test mixins or reusing test code?

johnr
Thanks, that's a nice simple example.  The tricky problems only seem
to manifest when adding a custom @BeforeClass.  @Before and @After in
a custom mixin work as long as there's no ordering requirement.

I'm still interested to hear other recommendations from the group.  Is
anyone using multiple test mixins instead of extending
GrailsUnitTestMixin?
e.g. @TestMixin([CustomTestMixin, AnotherCustomTestMixin])
By the way, this works with `grails test-app` but not with the GGTS
junit test runner.

-John

On Tue, May 14, 2013 at 2:20 PM, Sergio Michels
<[hidden email]> wrote:
> Grails GSON provide a TestMixin, maybe you can use that as start point.
> https://github.com/robfletcher/grails-gson/blob/master/src/groovy/grails/plugin/gson/test/GsonUnitTestMixin.groovy
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Custom test mixins or reusing test code?

Andrew Eisenberg-2

By the way, this works with `grails test-app` but not with the GGTS
junit test runner.


What version of GGTS are you using and what is the problem that you are seeing?
Reply | Threaded
Open this post in threaded view
|

Re: Custom test mixins or reusing test code?

Andrew Eisenberg
In reply to this post by johnr
> By the way, this works with `grails test-app` but not with the GGTS
> junit test runner.

What version of GGTS are you using and what is the problem that you are seeing?

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email