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

GRAILS-10538: Unit test of service doesn't work with @Transactional annotation #1501

Closed
graemerocher opened this issue Sep 25, 2013 · 4 comments

Comments

@graemerocher
Copy link
Member

Original Reporter: spade
Environment: Not Specified
Version: 2.3
Migrated From: http://jira.grails.org/browse/GRAILS-10538

To reproduce run

{code}
grails create-app new_app
grails create-service Foo

{code}
open %app_home%/test/unit/new_app/FooServiceSpec.groovy and edit the generated test to

{code}
void "test something"() {
expect: service.serviceMethod()
}
{code}

If I now run

{code}
grails test-app
{code}

I get the following output

{code}
| Running 1 unit tests...
| Running 1 unit tests... 1 of 1
| Failure: test something(new_app.FooServiceSpec)
| java.lang.NullPointerException
at new_app.FooServiceSpec.test something(FooServiceSpec.groovy:19)
| Completed 1 unit test, 1 failed in 0m 2s
{code}

If I now go to %app_home%/grails-app/services/new_app/FooService.groovy and remove the @transactional annotation and run

{code}
grails test-app
{code}

I get the following output

{code}
| Running 1 unit tests...
| Running 1 unit tests... 1 of 1
| Failure: test something(new_app.FooServiceSpec)
| Condition not satisfied:
service.serviceMethod()
| |
| null
new_app.FooService@4d905f1f
at new_app.FooServiceSpec.test something(FooServiceSpec.groovy:19)
| Completed 1 unit test, 1 failed in 0m 2s
{code}

and everything works as expected.

Workaround:
If I add the line

{code}
static transactional = true
{code}

to the service instead of the annotation the test still work and I still have the desired behaviour of my service

@graemerocher
Copy link
Member Author

graemerocher said:
This is a unit testing issue with the @transactional annotation, not related to the JUnit test runner changes

@graemerocher
Copy link
Member Author

partyk1d24 said:
Still seeing this in my 2.3.7 project. I need to use static transactional = true to get unit tests working

@jamesdh
Copy link

jamesdh commented Jun 4, 2015

We we're seeing a similar issue in Jesque jobs where we had to add @transactional after upgrading to Hibernate 4 due to the lack of an open Session, and prepending...

@TestMixin(DomainClassUnitTestMixin)

to the class and then adding the TransactionManager manually a la...

jobUnderTest.transactionManager = getTransactionManager()

fixed it for us!

@ppazos
Copy link

ppazos commented Dec 13, 2015

@jamesdh I did that and I'm getting groovy.lang.MissingPropertyException: No such property: transactionManager for class: ehr.IndexDataJob

BTW, this behavior still appears on Grails 2.4.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants