Roo和Failsafe集成测试:BeanConfigurerSupport问题

时间:2014-09-16 17:38:36

标签: maven spring-roo maven-failsafe-plugin load-time-weaving integration-testing

案例:使用集成测试自动审核的Roo应用程序。

需要:使用PGSQL进行部署,使用HSQLDB进行集成测试。

选择:spring-agent / spring-instrument&在maven-failsafe-plugin配置中的argLine中的aspectjweaver,以便不耦合到某个tomcat安装(应该具有所需的jar和Context配置)

结果

  

BeanConfigurerSupport - 尚未设置BeanFactory   BeanConfigurerSupport:确保此配置程序在Spring中运行   容器。无法配置类型的bean   [com.model.UserIntegrationTest]。无需注射即可进行。

我注意到它使用了类加载器:WebappClassLoader

我还尝试了更改tomcat配置并在其libs中添加所需的jar的方法,但我无法使故障安全插件与远程tomcat协作。

有什么想法吗?

以下是与此问题相关的代码段。 获得SSCCE的最简单方法是获取spring roo示例,并尝试执行作为集成测试生成的* IntegrationTest roo,其中针对hsqldb进行故障安全以进行测试,并执行其他一些db for deploy。

pom.xml的一部分:

<properties>
    ...........
    <aspectj.version>1.8.2</aspectj.version>
<properties>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <outxml>true</outxml>
        <complianceLevel>1.7</complianceLevel>
        <source>1.7</source>
        <target>1.7</target>
        <weaveWithAspectsInMainSourceFolder>true</weaveWithAspectsInMainSourceFolder>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
                <!-- use this goal to weave all your main classes -->
                <goal>test-compile</goal>
                <!-- use this goal to weave all your test classes -->
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
        <!--see: https://issuetracker.springsource.com/browse/EBR-220-->
        <printSummary>false</printSummary>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
        <excludes>
            <exclude>${roo.aspect.test.files}</exclude>
            <exclude>${integration.test.files}</exclude>
        </excludes>
        <reuseForks>false</reuseForks>
        <forkCount>1</forkCount>
        <argLine>-javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03.jar -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.2/aspectjweaver-1.8.2.jar -javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</argLine>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
            <integration-test.url>http://localhost:8080/${project.build.finalName}/</integration-test.url>
        </systemPropertyVariables>
        <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
        <includes>
            <include>${integration.test.files}</include>
        </includes>
        <excludes>
            <exclude>${roo.aspect.test.files}</exclude>
        </excludes>
        <reuseForks>false</reuseForks>
        <forkCount>1</forkCount>
        <argLine>-javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.6.SEC03/spring-agent-2.5.6.SEC03.jar -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.2/aspectjweaver-1.8.2.jar -javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</argLine>
    </configuration>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>${server.url}/manager/text</url>
        <server>TomcatServer</server>
        <path>/${project.name}</path>
    </configuration>
    <executions>
        <execution>
            <id>start-tomcat</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>stop-tomcat</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>shutdown</goal>
            </goals>
        </execution>
    </executions>
</plugin> 

applicationContext.xml的一部分,其中定义了IT的配置文件:

<beans profile="integrationTests">

     <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        id="itdataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:ittests;shutdown=true"/> <property
        name="username" value="sa"/> <property name="password" value=""/> <property
        name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/> <property name="timeBetweenEvictionRunsMillis"
        value="1800000"/> <property name="numTestsPerEvictionRun" value="3"/> <property
        name="minEvictableIdleTimeMillis" value="1800000"/> </bean>

    <!--<jdbc:embedded-database id="itdataSource" type="HSQL" />-->
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnitForIntegrationTests" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="HSQL" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="dataSource" ref="itdataSource" />
    </bean>
</beans>     

将aspectj-maven-plugin更新为1.7,而不是我最初发布的消息,我得到了

[WARNING] advice defined in org.springframework.mock.staticmock.AbstractMethodMockingControl has not been applied [Xlint:adviceDidNotMatch]
    C:\Users\ckorakidis\.m2\repository\org\springframework\spring-aspects\3.2.6.RELEASE\spring-aspects-3.2.6.RELEASE.jar!org\springframework\mock\staticmock\AbstractMethodMockingControl.class:167

.......

[WARNING] this affected type is not exposed to the weaver: com.mbplc.cborms.db.UserRepository [Xlint:typeNotExposedToWeaver]
C:\workspace\poc\com\db\UserRepository_Roo_Jpa_Repository.aj:16

.....

[AppClassLoader@3972aa3f] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
2014-09-16 23:35:33,389 [main] DEBUG org.springframework.beans.factory.wiring.BeanConfigurerSupport - BeanFactory has not been set on BeanConfigurerSupport: Make sure this configurer runs in a Spring container. Unable to configure bean of type [com.model.UserIntegrationTest]. Proceeding without injection.

......

    classloader: WebappClassLoader
  context: /poc
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
ClassRealm[plugin>org.apache.tomcat.maven:tomcat7-maven-plugin:2.2, parent: sun.misc.Launcher$AppClassLoader@65450f1f]

    excludeUnlistedClasses: false
    JTA datasource: null
    Non JTA datasource: org.apache.commons.dbcp.BasicDataSource@70a3b90
    Transaction type: RESOURCE_LOCAL

1 个答案:

答案 0 :(得分:0)

当我尝试在Roo项目中使用 Spring配置文件时,我发现了类似的问题。

如果您真的需要使用其他数据库配置运行测试,那么使用Maven profiles该怎么办?

以示例:

  1. 使用applicationContext.xml值修改BasicDataSource以创建database.properties(并删除个人资料标记)
  2. pom.xml上声明两个maven配置文件:一个用于测试,另一个用于最终数据库配置。
  3. Configure resources tag on pom.xml to filter database.properties
  4. 修改database.properties并将值设置为pom属性
  5. 这样,您可以使用测试配置文件和HSQLB运行集成测试,使用真实数据库打包/运行您的应用程序,而无需使用 Spring配置文件

    祝你好运!