I have a working Spring 4.3 application with unit tests, and all is well. This has your traditional 4 layers: entity, dao, services, web-services (ws). I am also using the latest JUnit 4.12 to create unit tests within Spring. This is running dao test in eclipse (STS).
When the dao.jar is built, it will point to one database, but I want to test against a different database.
Under 'entity' I have a: src/main/resources/myapp-entity-context.xml which points to the database correctly.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
Under 'dao' I have a: src/main/resources/myapp-dao-context.xml which has the following code:
<import resource="classpath*:/spring/myapp-entity-context.xml" />
The dao unit test, under dao, has a base configuration as follows:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "classpath:/spring/agmednet-dao-context.xml" })
@Transactional
public class MyDaoTests extends TestCase
This all works great, but now, I want to have the unit tests access code from another database.
So, what I did was the following, copied: src/main/resources/myapp-entity-context.xml
to: src/test/resources/test-myapp-entity-context.xml
which points to a different database.
I use a Spring profile, all the properties for both databases are in that one properties file, so I know that is correct!
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${**unit-test.connection.url**}</value>
</property>
<property name="username">
<value>${**unit-test.connection.username**}</value>
</property>
<property name="password">
<value>${**unit-test.connection.password**}</value>
</property>
</bean>
So, you can see only the url, username, and password have changed.
I also copied:
src/main/resources/myapp-dao-context.xml to: src/test/resources/test-myapp-dao-context.xml and this now imports the following:
<import resource="classpath*:/spring/test-myapp-entity-context.xml" />
And now the unit test was changed to look at:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "classpath*:/spring/test-myapp-dao-context.xml" })
@Transactional
public class MyDaoTests extends TestCase
So, unit test should be pulling in the correct context xml file.
Before I made any of these changes, the code was working. Now, the error message I gets is as follows:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.myapp.server.dao.common.MyDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myapp.server.dao.common.UserAccountDao com.myapp.server.dao.BaseDaoTests.userAccountDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.server.dao.common.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
So, at first I didn't think I had to rename from:
myapp-entity-context.xml to test-myapp-entity-context.xml myapp-dao-context.xml to test-myapp-dao-context.xml
but this just makes it easier to differentiate between the two.
I also read about how the test/resources gets copied to /test-classes So, I made sure when I do a 'clean', I do a refresh to make sure the file changes are there.
At this point, I don't know quite what else to do. If I can provide any more information, please let me know. Thanks!
Aucun commentaire:
Enregistrer un commentaire