0

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

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

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

结果

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

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

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

任何的想法?

以下是与该问题相关的代码片段。拥有 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> 

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
4

1 回答 1

0

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

If you really need to run test with other DB config, What about to use Maven profiles isntead?

By example:

  1. Modify you applicationContext.xml to create BasicDataSource using database.properties values (and remove the profile tag)
  2. Declare two maven profiles on pom.xml: one for test and another with final DB configuration.
  3. Configure resources tag on pom.xml to filter database.properties
  4. Modify database.properties and set values to pom properties

This way, you can run integration test with HSQLB using the test profile and the other profile to package/run your application with the real DB without use Spring profiles.

Good luck!

于 2014-09-17T06:53:42.613 回答