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

Problem Testing Data Services #1020

Closed
osscontributor opened this issue Oct 28, 2017 · 2 comments
Closed

Problem Testing Data Services #1020

osscontributor opened this issue Oct 28, 2017 · 2 comments

Comments

@osscontributor
Copy link
Member

See https://github.com/jeffbrown/servicetestproblem

Domain:

package servicetestproblem

class Person {
    String name
}

Data Service:

package servicetestproblem

import grails.gorm.services.Service

@Service(Person)
abstract class PersonService {
    abstract List<Person> list()
}

Unit test:

package servicetestproblem

import grails.testing.gorm.DataTest
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification

class PersonServiceSpec extends Specification implements ServiceUnitTest<PersonService>, DataTest {

    def setupSpec() {
        mockDomain Person
    }

    void 'first test'() {
        expect:
        service.list().size() == 0
    }

    void 'second test'() {
        expect:
        service.list().size() == 0
    }
}

There is no reason to have a unit test like that of course but this is a simple example that demonstrates the problem. The second test method will fail with the following:

Condition failed with Exception:

service.list().size() == 0
|
java.lang.reflect.InvocationTargetException

	at servicetestproblem.PersonServiceSpec.second test(PersonServiceSpec.groovy:20)
Caused by: java.lang.reflect.InvocationTargetException
	at grails.testing.services.ServiceUnitTest$Trait$Helper.mockArtefact(ServiceUnitTest.groovy:66)
	at org.grails.testing.ParameterizedGrailsUnitTest$Trait$Helper.getArtefactInstance(ParameterizedGrailsUnitTest.groovy:48)
	at grails.testing.services.ServiceUnitTest$Trait$Helper.getService(ServiceUnitTest.groovy:83)
	... 1 more
Caused by: java.lang.IllegalStateException: Could not register object [servicetestproblem.$PersonServiceImplementation@42339ea2] under bean name 'personService': there is already object [servicetestproblem.$PersonServiceImplementation@42339ea2] bound
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:130)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:936)
	at grails.testing.gorm.DataTest$Trait$Helper.mockDataService(DataTest.groovy:101)
	... 4 more
@osscontributor
Copy link
Member Author

I haven't investigated to know if this is a GORM problem or a problem with https://github.com/grails/grails-testing-support.

@osscontributor
Copy link
Member Author

Moving to grails/grails-testing-support#18

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

No branches or pull requests

1 participant