【问题标题】:Roo and Failsafe integration tests: BeanConfigurerSupport issueRoo 和 Failsafe 集成测试:BeanConfigurerSupport 问题
【发布时间】:2014-09-16 17:38:36
【问题描述】:

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

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

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

结果

BeanConfigurerSupport - BeanFactory 尚未设置 BeanConfigurerSupport:确保此配置器在 Spring 中运行 容器。无法配置类型的 bean [com.model.UserIntegrationTest]。继续进行而不注入。

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

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

有什么想法吗?

以下是与问题相关的代码 sn-ps。 拥有 SSCCE 的最简单方法是获取 spring roo 示例并尝试执行 *IntegrationTest roo 作为集成测试生成,并针对用于测试的 hsqldb 和用于部署的其他一些 db 进行故障保护。

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> 

为 IT 定义配置文件的 applicationContext.xml 的一部分:

<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

【问题讨论】:

  • 请发布代码和配置以使问题可重现,最好提供SSCCE

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


【解决方案1】:

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

如果您确实需要使用其他数据库配置运行测试,那么使用Maven profiles istead 怎么样?

举例:

  1. 修改您的 applicationContext.xml 以使用 database.properties 值创建 BasicDataSource(并删除配置文件标签)
  2. pom.xml 上声明两个 maven 配置文件:一个用于测试,另一个用于最终数据库配置。
  3. Configure resources tag on pom.xml to filterdatabase.properties
  4. 修改database.properties并将值设置为pom属性

这样,您可以使用测试配置文件和其他配置文件运行与 HSQLB 的集成测试,以使用真实数据库打包/运行您的应用程序,而无需使用 Spring 配置文件

祝你好运!

【讨论】:

  • 嗨@jmvivo,感谢您的回复。我知道这种方法,如果弹簧配置文件不起作用,那将是我的最终方法。我认为最好保持 pom 清洁这些东西,但我可能是错的,我现在不知道哪个更好。目前我在正确配置 aspectj 并在“mvn verify”中使用它时遇到问题。我更新了一些依赖项(如您在上面看到的)roo 添加,以便使用故障安全插件。这导致 aspectj 出现问题(请参阅我的上一个输出)
  • 在您的输出中,我可以看到一些 aspecj 警告(但不是错误)。最后出现同样的Spring初始化异常BeanFactory has not been set on BeanConfigurerSupport。这就是测试失败的原因(它不能注入 DOD 服务来运行测试)。应用我的建议,您只需执行 mvn -Pverify verifymvn clean tomcat:run 即可执行。
  • 所以我不确定为什么改变创建数据源的方式会改变结果。我尝试了这种方法并遇到了同样的 BeanConfigurerSupport 问题。它看起来更像是一个与 aspectj 相关的问题,而不是一个 db-configuration 问题..
  • 问题出在spring上下文初始化中(那里没有aspectj)。证明它的简单方法:删除beans 标签并只保留标准配置(之前备份数据库;))。然后运行集成测试。我相信这会奏效。
  • 我在移除弹簧型材时移除了 beans 标签。我使用 spring 4.0 并且 applicationContext.xml 开始如下:
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
相关资源
最近更新 更多