Announcement Announcement Module
Collapse
No announcement yet.
Failed to load ApplicationContext(Maven+Spring+JPA+Junit) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failed to load ApplicationContext(Maven+Spring+JPA+Junit)

    I encounter a strange issue that after I add AOP for transaction, the test module can not work. Detail as folow.

    when running mvn test
    Error,
    Tests in error:
    testUserDao(com.cvt.smarthome.UnitTest): Failed to load ApplicationContext
    testAccountRegister(com.cvt.smarthome.IntegrationT est): Failed to load ApplicationContext
    testSystemApiVersion(com.cvt.smarthome.Integration Test): Failed to load ApplicationContext

    log in com.cvt.smarthome.IntegrationTest.txt
    Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 1.815 sec <<< FAILURE!
    testAccountRegister(com.cvt.smarthome.IntegrationT est) Time elapsed: 1.105 sec <<< ERROR!
    java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:157)
    at org.springframework.test.context.web.ServletTestEx ecutionListener.setUpRequestContextIfNecessary(Ser vletTestExecutionListener.java:103)
    .........
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.maven.surefire.util.ReflectionUtils.inv okeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$P roviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.i nvokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.r unSuitesInProcessWhenForked(SurefireStarter.java:1 07)
    at org.apache.maven.surefire.booter.ForkedBooter.main (ForkedBooter.java:68)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSour ceInitializer#0': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.Composite DatabasePopulator] while setting bean property 'databasePopulator'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#59': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.ResourceD atabasePopulator] while setting bean property 'populators' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#59': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.config.SortedResourcesFac toryBean] while setting bean property 'scripts'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#59': Post-processing of the FactoryBean's object failed; nested exception is java.lang.AssertionError
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:282)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:126)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1391)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1132)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 95)
    at ..........

    what I added into ApplicationContext.xml(red marked)
    <?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="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schem...g-jdbc-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schem...ng-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schem...ng-jpa-1.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.cvt.smarthome" />

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <!-- <context:load-time-weaver/> -->

    <bean class="org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor" />

    <!-- Spring Data Jpa配置 -->
    <jpa:repositories base-package="com.cvt.smarthome" transaction-manager-ref="transactionManager"
    entity-manager-factory-ref="entityManagerFactory" />

    <!-- Jpa 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- <tx:method name="get*" read-only="true"/> -->
    <tx:method name="*Router" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aopointcut id="Router" expression="execution(* com.cvt.smarthome..*Router.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="Router"/>
    </aop:config>


    <!-- 服务路由配置 -->
    <bean id="service" class="com.cvt.smarthome.router.ServiceRouter" abstract="true" />
    <bean id="account" class="com.cvt.smarthome.core.user.UserRouter" />
    <bean id="user" class="com.cvt.smarthome.core.user.UserRouter" />
    <bean id="family" class="com.cvt.smarthome.core.family.FamilyRouter" />

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
    </bean>

    <beans profile="production">
    ......
    </beans>

    <beans profile="test">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:smart" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    </bean>

    <jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:test.sql" />
    </jdbc:initialize-database>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan" value="com.cvt.smarthome.entity" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect"></property>
    </bean>
    </property>
    <property name="jpaProperties">
    <props>
    <prop key="hibernate.ejb.naming_strategy">org.hibernate. cfg.ImprovedNamingStrategy</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    </props>
    </property>
    </bean>
    </beans>

    </beans>


    code for Test
    @RunWith(SpringJUnit4ClassRunner.class)
    @ActiveProfiles("test")
    @WebAppConfiguration
    @ContextConfiguration(locations = {
    "classpath*:applicationContext.xml",
    "file:src/main/webapp/WEB-INF/smart-servlet.xml" })
    public class IntegrationTest {
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build ();
    }

    @Test
    public void testSystemApiVersion() throws Exception {
    final String url = "/system/api_version.json";
    final String expect_version = "0.2.1";

    this.mockMvc.perform(get(url))
    .andExpect(status().isOk())
    .andExpect(content().contentType(MediaType.APPLICA TION_JSON))
    .andExpect(jsonPath("$.version").value(expect_vers ion));
    }
    }

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable ....

    The needed information isn't in the stacktrace so repost with the mentioned tags.

    Some pointers, not related to y our problem

    1. context:annotation-config is already implied by context:component-scan so can be removed
    2. Registration of the PersistenceExceptionTranslationPostProcessor is automatically done by context:component-scan when JPA is detected on the classpath.
    3. It is adviced to use the versionless schema names, currently you are mixing different schema versions in your context (2.5 and 3.2).

    Comment

    Working...
    X