Announcement Announcement Module
Collapse
No announcement yet.
problem running tests with multiple application contexts and loadtime weaving Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem running tests with multiple application contexts and loadtime weaving

    Hi all,

    we are facing problems running our Integration tests with load time weaving:
    • our tests all extend org.springframework.test.context.junit4.AbstractJU nit4SpringContextTests
    • some tests use applicationContext1, others use applicationContext2, both having beans with the same id
    • Some of the beans are woven onto domain objects
    • we use aspectjweaver as the java agent

    When we are running the tests, we notice that the bean woven into the domain object differs from the bean defined in the application context loaded by the test. We know the application contexts are loaded only once based on the unique set of locations (which we greatly appreciate for performance reasons). The problem however is that the bean woven into our domain objects is the one from the last loaded application context.

    We were able to reproduce the problem with some simple unit tests:
    Our test suite:
    Code:
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses( { // 
    WeavingTest1.class, WeavingTest2.class, WeavingTest3.class })
    public class FailingTestSuite {
    
    }
    Test class 1:
    Code:
    package springsample;
    
    import static junit.framework.Assert.assertSame;
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    
    @ContextConfiguration(locations = "appContext1.xml")
    public class WeavingTest1 extends AbstractJUnit4SpringContextTests {
    
        @Resource
        private Adres adres;
    
        @Test
        public void testje() {
            Person person = new Person();
    
            System.out.println("resourceAdres: " + adres);
            System.out.println("personAdres: " + person.getAdres());
            assertSame(adres, person.getAdres());
        }
    }
    Test class 2: (notice that this is equal to Test class 1 except for the loaded application context)
    Code:
    package springsample;
    
    import static junit.framework.Assert.assertSame;
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    
    @ContextConfiguration(locations = "appContext2.xml")
    public class WeavingTest1 extends AbstractJUnit4SpringContextTests {
    
        @Resource
        private Adres adres;
    
        @Test
        public void testje() {
            Person person = new Person();
    
            System.out.println("resourceAdres: " + adres);
            System.out.println("personAdres: " + person.getAdres());
            assertSame(adres, person.getAdres());
        }
    }
    Test class 3 (extends Test class 1, so it uses the same application context)
    Code:
    package springsample;
    
    public class WeavingTest3 extends WeavingTest1 {
    
    }
    the used domain class:
    Code:
    package springsample;
    
    import org.springframework.beans.factory.annotation.Configurable;
    
    @Configurable
    public class Person {
    
        private Adres adres;
    
        public void setAdres(Adres adres) {
            this.adres = adres;
        }
    
        public Adres getAdres() {
            return adres;
        }
    
    }
    the woven class:
    Code:
    package springsample;
    
    public class Adres {
    
    }
    the used applicationContext 1:
    Code:
    <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:spring-configured />
    	
    	<bean class="springsample.Person" scope="prototype">
    	<property name="adres" ref="adres" /> 
    	</bean>
    
    	<bean id="adres" class="springsample.Adres" >
    	</bean>
    </beans>
    the used applicationContext 2:
    Code:
    <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:spring-configured />
    	
    	<bean class="springsample.Person" scope="prototype">
    	<property name="adres" ref="adres" /> 
    	</bean>
    
    	<bean id="adres" class="springsample.Adres" >
    	</bean>
    </beans>
    the output when running our test suite looks like this:
    resourceAdres: springsample.Adres@121df2a
    personAdres: springsample.Adres@121df2a

    resourceAdres: springsample.Adres@4c71d2
    personAdres: springsample.Adres@4c71d2

    resourceAdres: springsample.Adres@121df2a
    personAdres: springsample.Adres@4c71d2

    Notice the differences between the resourceAdres and personAdres in the latest test run.

    What we expected was that the adres bean woven using aspectJ would be the same as the adres from the application context in the running test.

    Any help would be appreciated
Working...
X