-
@Value in unit test
Hi,
it looks like I need help in using @Value annotation in Spring unit tests. My unit test extends AbstractJUnit4SpringContextTests and everything works fine including the @Autowired annotation.
But if I tried to use the new Spring 3.0 feature to inject property from config file like this:
Code:
@Value("${timeout}")
private String timeout;
It throws the followig exception:
Code:
ERROR - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@19e8f17] to prepare test instance [com.aspectworks.hammer.invoice.selenium.WorkflowTest@c44b88]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aspectworks.hammer.invoice.selenium.WorkflowTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.aspectworks.hammer.invoice.selenium.WorkflowTest.setTimeout(java.lang.String); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.aspectworks.hammer.invoice.selenium.WorkflowTest' is defined
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:333)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
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.BeanCreationException: Could not autowire method: public void com.aspectworks.hammer.invoice.selenium.WorkflowTest.setTimeout(java.lang.String); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.aspectworks.hammer.invoice.selenium.WorkflowTest' is defined
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:608)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.aspectworks.hammer.invoice.selenium.WorkflowTest' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:509)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:827)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:696)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:559)
... 28 more
-
Hello,
We experience exactly the same error.
@Value(${property} works within the webapplication but not within JUnit Tests.
Is there someone else who experience this ? Should i fill a jira issue ?
Edit : I checked Jira and found this issue : http://jira.springframework.org/browse/SPR-6611.
I upgraded to 3.0.1 but it doesn't help.
-
@Value will only work for beans defined in the applicationcontext as those values are replaced/detected with a BeanFactoryPostProcessor. So using @Value in a Test class isn't going to be possible.
-
-
I think this feature should be available in unit tests because it helps to simplify test development and because we should reduce "magic" in Spring behavior. Developers simply expect @Value to work.
-
Well I'm a developer and I didn't expect it to work, however that probably is due to the fact that I know the underlying mechanism. The @Value works only for beans defined in the app context as those are injected/replaced by modifying the BeanDefinition for a TestCase there is no bean definition (hence the exception NoSuchBeanDefinitionException).
It works in the same way the PropertyPlaceHolderConfigurer works and that doesn't work for a testcase either.
-
Actually you can solve this in two ways:
1. Make private field protected and place the test within the same package, this way you can access the field in your test (Not nice)
2. Use reflection:
http://tutorials.jenkov.com/java-ref...d-methods.html