0

I have dao layer which is in project A and project B is application layer which uses project A as it's dependency

I can see the classes from project A and coded perfectly in project B using those classes.

Now I want to do JUnit testing

in A (MyTest.java)

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ContextConfiguration(locations = { "classpath*:reb/spring/persistenceContextTest.xml", "classpath*:rev/spring/modelContextTest.xml" })
public class MyTest extends AbstractTransactionalJUnit4SpringContextTests {
      @Autowired
      protected HibernateTemplate hibernateTemplate;
      @Autowired
      protected DAOImpl dao;
         //CODE here
}

Everything is fine here. All properties are injected properly and no issues.

Now I need to test Application Layer {project b}

@ContextConfiguration(locations = { "classpath*:reb/spring/applicationContextTest.xml" })
public class MyAnotherTest extends AbstractTransactionalJUnit4SpringContextTests {
     @Autowired
     protected HibernateTemplate hibernateTemplate;
     @Autowired
     protected DAOImpl dao;
}

and in applicationContextTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <import resource="classpath*:reb/spring/persistenceContextTest.xml" />
    <import resource="classpath*:reb/spring/modelContextTest.xml" />

</beans>

but when I run the B test, Spring is unable to find the properties defined in project A. How do i achieve this?

Update I moved test xmls to Src folder so that maven recognize in Project B and indeed it recognized. however I am getting below error still

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    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.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:49)
    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: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.BeanCreationException: Error creating bean with name 'org.apache.cxf.transport.servlet.ServletTransportFactory' defined in class path resource [META-INF/cxf/cxf-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
    ... 38 more
RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • Your application context name implies, that you are using a test context. If you are using maven and `applicationContextTest.xml` is in the `test`folder of project a, it will not be visible in project b, as `test`resources and sources are only local. If this does not help, could you share the stack trace with us? – Stefan Dec 11 '12 at 06:30
  • Yes I am using maven. But how do i test them? – RaceBase Dec 11 '12 at 06:32

2 Answers2

2

if you are using maven, then any resources and sources in src/test are local the individual module, i.e. anything in projectA/src/test will be hidden from projectB. There are multiple options available:

  • Move Test Resources: I would discourage you from doing this, but basically you can move your applicationContextTest.xml to the src/main folder, but then you have test code in your binary deliverables.

  • Replicate Test Resources: This is not as bad as the previous suggestion, when you copy any bean setup from projectA to projectB. This usually implies that you are creating test application contexts for your unit tests and that you have different application contexts for your real application.

  • Delete all Test Application Contexts: This would be the cleanest solution, if you dont have any test context you are forced to test the real thing. If you need environment specific properties you can use PropertyPlaceHolders, if you need different beans (Connection Pools, etc.) you can use Profiles.

Have a look at the new Spring 3.1 Feature for Profiles: http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/

Stefan
  • 990
  • 1
  • 6
  • 10
  • I followed the Second option, Copied my test xmls to Src folder and now it's recognizing however Spring is throwing error when running from Project B and no issues with same config from Project A – RaceBase Dec 11 '12 at 07:11
  • can you enlighten us with a stack trace? :-) – Stefan Dec 11 '12 at 07:12
  • Are you sure, that you copied all imports from applicationContextTest.xml as well? Double Check your xml files for completeness. Also you must ensure that you habe all your dependencies as well, test scoped dependencies are not transitive in maven! – Stefan Dec 11 '12 at 07:29
  • Yes. otherwise the Test from Project A should have failed. but it's successful and I am using the same imports in Project B now – RaceBase Dec 11 '12 at 07:36
  • If some spring configuration from Project A which you copied to Project B requires additional configurations or JARs which you did not copy to Project B, tests will succeed in Project A but not in Project B - not? – Stefan Dec 11 '12 at 07:39
  • that's true. But Project B uses project A as library and rest of the libraries whatever requried are available – RaceBase Dec 11 '12 at 08:02
  • I am afraid there is nothing more I can do - something got lost from copying the stuff around. Maybe you can get a colleague and four eye compare the two projects (especially used dependencies and spring configs?) – Stefan Dec 11 '12 at 08:06
0

Your test persistence context xml name is same in project A and project B. Can you try changing the name persistenceContextTest.xml in project A to persistenceContextTestA.xml to be sure that the problem is not because of duplicate file name.

Parvez
  • 631
  • 1
  • 6
  • 19
  • I don't have another persistentContextTest – RaceBase Dec 11 '12 at 07:13
  • Do you mean persistenceContextTest.xml is present in project A. Where is your applicationcontext file for project B? Can you also post contents of persistenceContextDump.xml? – Parvez Dec 11 '12 at 08:03
  • I have changed *Test.xml to *Dump.xml and copied to main/src folder. For project B, the applicationContextTest is in main/test folder. http://sebsauvage.net/paste/?9d25de7655b4d082#rTFd16QezK5Bkg1nvVpinhIQu3GkHOTUYc/WUcKEWHU= – RaceBase Dec 11 '12 at 08:07
  • Can you move your properties file to classpath and refer something like this from your xml. – Parvez Dec 11 '12 at 09:25