Spock-spring configuration class

1,161 views
Skip to first unread message

Zachary Carter

unread,
Mar 20, 2013, 2:18:27 PM3/20/13
to spockfr...@googlegroups.com
Hello,

Spring application configuration class I'm trying to use in my spock-spring test. I have all the necessary jars (spring-beans, spring-test, spock-spring) on my classpath.

I've created a very simple test class - 

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration

import spock.lang.Specification

import com.example.configuration.AppConfig

@ContextConfiguration(classes=[AppConfig])
class PartnerClientIntegrationTest extends Specification {

@Autowired
PartnerClient partnerClient
def setup() {
}
def "test"() {
expect:
println partnerClient
}

}

And here's what my app config looks like - 

@Slf4j
@Configuration
@ImportResource(
['classpath:springmvc-resteasy.xml',
 'classpath:com/example/datasource-context.xml'])
@Import(AmqpContext)
@ComponentScan(['com.example'])
public class AppConfig implements ApplicationContextAware {
   ...
}

When I run my test I encounter the following errors - 

INFO  TestContextManager - @TestExecutionListeners is not present for class [class com.example.dris.sfdc.client.PartnerClientIntegrationTest]: using defaults.
INFO  XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [com/example/dris/sfdc/client/PartnerClientIntegrationTest-context.xml]
ERROR TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@361cb7a1] to prepare test instance [com.example.dris.sfdc.client.PartnerClientIntegrationTest@175bc6c8]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:181)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.spockframework.spring.SpringTestContextManager.prepareTestInstance(SpringTestContextManager.java:49)
at org.spockframework.spring.SpringInterceptor.interceptSetupMethod(SpringInterceptor.java:42)
at org.spockframework.runtime.extension.AbstractMethodInterceptor.intercept(AbstractMethodInterceptor.java:28)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:319)
at org.spockframework.runtime.BaseSpecRunner.invokeSetup(BaseSpecRunner.java:279)
at org.spockframework.runtime.BaseSpecRunner.doRunIteration(BaseSpecRunner.java:255)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:330)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:311)
at org.spockframework.runtime.BaseSpecRunner.runIteration(BaseSpecRunner.java:223)
at org.spockframework.runtime.BaseSpecRunner.initializeAndRunIteration(BaseSpecRunner.java:214)
at org.spockframework.runtime.BaseSpecRunner.runSimpleFeature(BaseSpecRunner.java:205)
at org.spockframework.runtime.BaseSpecRunner.doRunFeature(BaseSpecRunner.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:330)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:311)
at org.spockframework.runtime.BaseSpecRunner.runFeature(BaseSpecRunner.java:175)
at org.spockframework.runtime.BaseSpecRunner.runFeatures(BaseSpecRunner.java:152)
at org.spockframework.runtime.BaseSpecRunner.doRunSpec(BaseSpecRunner.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:330)
at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:311)
at org.spockframework.runtime.BaseSpecRunner.runSpec(BaseSpecRunner.java:91)
at org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:82)
at org.spockframework.runtime.Sputnik.run(Sputnik.java:63)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/example/dris/sfdc/client/PartnerClientIntegrationTest-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/example/dris/sfdc/client/PartnerClientIntegrationTest-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:162)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:70)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:128)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:172)
... 47 more
Caused by: java.io.FileNotFoundException: class path resource [com/example/dris/sfdc/client/PartnerClientIntegrationTest-context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 56 more



Anyone have any ideas why I'm getting this error?

Thanks,

-Zach

Erick Dovale

unread,
Mar 20, 2013, 2:28:56 PM3/20/13
to spockfr...@googlegroups.com

Here is the reason:


Caused by: java.io.FileNotFoundException: class path resource [com/example/dris/sfdc/client/PartnerClientIntegrationTest-context.xml] cannot be opened because it does not exist

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Zachary Carter

unread,
Mar 20, 2013, 2:32:40 PM3/20/13
to spockfr...@googlegroups.com
Hi edovale,

I understand that's what's causing the top level error to be thrown, but if you look in my code I'm specifying a class configuration for the configuration context annotation.

It shouldn't be looking for XML files if I'm specifying a class configuration as far as I'm aware.

Thanks,

-Zach

Erick Dovale

unread,
Mar 20, 2013, 2:33:46 PM3/20/13
to spockfr...@googlegroups.com

Could it be that from your test context the missing file is being imported maybe?

Zachary Carter

unread,
Mar 20, 2013, 2:44:33 PM3/20/13
to spockfr...@googlegroups.com
I don't think so...

Here's an even simpler example :

Test - 

@ContextConfiguration(classes=[TestConfig])
class PartnerClientIntegrationTest extends Specification {

@Resource
String test
def setup() {
}
def "test"() {
expect:
assert test
}
}

TestConfig - 

@Slf4j
@Configuration
public class TestConfig implements ApplicationContextAware{

ApplicationContext applicationContext
Context context = new InitialContext().lookup("java:/comp/env")
@Bean
String test(){
return "Test"
}
}

Same error is thrown...
Reply all
Reply to author
Forward
0 new messages