grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 950 forks source link

Unit test : Invalid connection [myDatasource] configured for class [class xxx.MyDomain] #10888

Closed bdbogjoe closed 6 years ago

bdbogjoe commented 6 years ago

I'm trying to execute unit test on one Domain but i have exeption while running tests

1) execute grails test-app 2) receiving error :

org.grails.datastore.mapping.core.exceptions.ConfigurationException: Invalid connection [myDataSource] configured for class [class joe.Test]
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getDatastoreForQualifier(SimpleMapDatastore.java:256)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getStaticApi(SimpleMapDatastore.java:226)
    at org.grails.datastore.gorm.GormEnhancer.registerEntity(GormEnhancer.groovy:140)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$3.persistentEntityAdded(SimpleMapDatastore.java:218)
    at org.grails.datastore.mapping.model.AbstractMappingContext.addPersistentEntities(AbstractMappingContext.java:288)
    at grails.testing.gorm.DataTest$Trait$Helper.mockDomains(DataTest.groovy:78)
    at org.grails.testing.gorm.spock.DataTestSetupSpecInterceptor.configureDataTest(DataTestSetupSpecInterceptor.groovy:88)
    at org.grails.testing.gorm.spock.DataTestSetupSpecInterceptor.intercept(DataTestSetupSpecInterceptor.groovy:41)
    at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:147)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:129)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Environment Information

Example Application

I created simple application from scratch : https://github.com/bdbogjoe/unit-test-multiple-datasource

bdbogjoe commented 6 years ago

seems related to #10856 but cannot find solution from @graemerocher post

jameskleeh commented 6 years ago

Simply create your own SimpleMapDatastore and call the variable dataStore so it will be referenced from the trait.

@Shared @AutoCleanup SimpleMapDatastore dataStore = new SimpleMapDatastore([ConnectionSource.DEFAULT, "one"], Player)

https://github.com/grails/grails-testing-support/blob/7b22fbf152091f71151e91052a2190c9a6fb63d9/grails-gorm-testing-support/src/main/groovy/grails/testing/gorm/DataTest.groovy#L105

jameskleeh commented 6 years ago

Submitted a PR to your project that causes the test to pass

https://github.com/bdbogjoe/unit-test-multiple-datasource/pull/3

bdbogjoe commented 6 years ago

thanks for your PR, i was aware about this trick, i will use it for now, but according to me this can be automcatic as we know list of mockDomains, so dataStore can be built will list of required dataSource names

But if we need dataStore somewhere and we get it from applicationContext it will return the wrong one

like : getApplicationContext().getBean(AbstractDatastore)

RxL-Hemlata commented 3 months ago

When I changes I got this error Error creating bean with name 'grailsApplicationPostProcessor': Initialization of bean failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: 0 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'grailsApplicationPostProcessor': Initialization of bean failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: 0 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524) at grails.test.runtime.GrailsApplicationTestPlugin.createMainContext(GrailsApplicationTestPlugin.groovy:121) at grails.test.runtime.GrailsApplicationTestPlugin.initGrailsApplication(GrailsApplicationTestPlugin.groovy:84) at grails.test.runtime.GrailsApplicationTestPlugin.onTestEvent(GrailsApplicationTestPlugin.groovy:335) at grails.test.runtime.TestRuntime.deliverEvent(TestRuntime.groovy:295) at grails.test.runtime.TestRuntime.executeEventLoop(TestRuntime.groovy:280) at grails.test.runtime.TestRuntime.processEvents(TestRuntime.groovy:265) at grails.test.runtime.TestRuntime.doPublishEvent(TestRuntime.groovy:238) at grails.test.runtime.TestRuntime.publishEvent(TestRuntime.groovy:211) at grails.test.runtime.TestRuntime.getValue(TestRuntime.groovy:122) at grails.test.runtime.GrailsApplicationTestPlugin.onTestEvent(GrailsApplicationTestPlugin.groovy:328) at grails.test.runtime.TestRuntime.deliverEvent(TestRuntime.groovy:295) at grails.test.runtime.TestRuntime.processEvents(TestRuntime.groovy:264) at grails.test.runtime.TestRuntime.doPublishEvent(TestRuntime.groovy:248) at grails.test.runtime.TestRuntime.publishEvent(TestRuntime.groovy:211) at grails.test.runtime.TestRuntimeJunitAdapter.before(TestRuntimeJunitAdapter.groovy:106) at grails.test.runtime.TestRuntimeJunitAdapter$1$2.evaluate(TestRuntimeJunitAdapter.groovy:44) at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38) at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87) at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88) at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37) at grails.test.runtime.TestRuntimeJunitAdapter$3$4.evaluate(TestRuntimeJunitAdapter.groovy:73) at org.spockframework.runtime.extension.builtin.ClassRuleInterceptor.intercept(ClassRuleInterceptor.java:38) at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:147) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:129) at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 at grails.test.runtime.GrailsApplicationTestPlugin.resolveTestCallback_closure4(GrailsApplicationTestPlugin.groovy:225) at groovy.lang.Closure.call(Closure.java:420) at groovy.lang.Closure.call(Closure.java:436) at grails.test.runtime.GrailsApplicationTestPlugin.executeDoWithConfigCallback(GrailsApplicationTestPlugin.groovy:207) at grails.test.runtime.GrailsApplicationTestPlugin.customizeGrailsApplication(GrailsApplicationTestPlugin.groovy:179) at grails.test.runtime.GrailsApplicationTestPlugin.registerGrailsAppPostProcessorBean_closure2(GrailsApplicationTestPlugin.groovy:151) at groovy.lang.Closure.call(Closure.java:420) at groovy.lang.Closure.call(Closure.java:436) at grails.test.runtime.GrailsApplicationTestPlugin$TestRuntimeGrailsApplicationPostProcessor.customizeGrailsApplication(GrailsApplicationTestPlugin.groovy:380) at grails.boot.config.GrailsApplicationPostProcessor.initializeGrailsApplication(GrailsApplicationPostProcessor.groovy:93) at grails.boot.config.GrailsApplicationPostProcessor.setApplicationContext(GrailsApplicationPostProcessor.groovy:225) at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121) at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1622) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ... 50 more