使用Arquillian测试Spring启动应用程序

时间:2015-02-17 16:28:29

标签: java spring-boot jboss-arquillian

我正在构建一个基于Spring Boot v1.2.1的应用程序并在WildFly 8.2上运行它。我想使用Arquillian来满足我的测试需求,因为它比插入application.properties文件更简单,无法进行集成测试。

我无法启动,因为我认为War File实际上并未在应用程序启动之前部署。

pom.xml的Arquillian部分低于config

    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-api-maven</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-service-container-spring</artifactId>
        <version>${arquillian.spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-service-deployer-spring-3</artifactId>
        <version>${arquillian.spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-service-integration-spring-inject</artifactId>
        <version>${arquillian.spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-service-integration-spring-javaconfig</artifactId>
        <version>${arquillian.spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-warp-spring</artifactId>
        <version>${arquillian.spring.version}</version>
        <scope>test</scope>
    </dependency> 

.....

    <profile>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <id>wildfy82-embedded</id>
        <!-- the dependencies for Wildfly8.2  -->
        <repositories>
            <repository>
                <id>JBOSS Maven Repository</id>
                <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
            </repository>
        </repositories>
        <dependencies>
            <!--
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-embedded</artifactId>
                <version>8.2.0.Final</version>
            </dependency>
            -->
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-embedded</artifactId>
                <version>8.2.0.Final</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <systemProperties>
                            <property>
                                <name>java.util.logging.manager</name>
                                <value>org.jboss.logmanager.LogManager</value>
                            </property>
                        </systemProperties>
                    </configuration>
                </plugin>
                <!-- You need the maven dependency plugin to download locally a zip with the server, unless you provide your own, it will download under the /target directory -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.10</version>
                    <executions>
                        <execution>
                            <id>unpack</id>
                            <phase>process-test-classes</phase>
                            <goals>
                                <goal>unpack</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>org.wildfly</groupId>
                                        <artifactId>wildfly-dist</artifactId>
                                        <version>8.2.0.Final</version>
                                        <type>zip</type>
                                        <overWrite>false</overWrite>
                                        <outputDirectory>target</outputDirectory>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <!-- Fork every test because it will launch a separate AS instance -->
                        <forkMode>always</forkMode>
                        <systemPropertyVariables>
                            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                            <!-- the maven dependency plugin will have already downloaded the server on /target -->
                            <jboss.home>C:/rearch/wildfly-8.2.0.Final</jboss.home>
                            <module.path>C:/rearch/wildfly-8.2.0.Final/modules</module.path>
                        </systemPropertyVariables>
                        <redirectTestOutputToFile>false</redirectTestOutputToFile>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

然后我的arquillian.xml看起来如下

<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
        http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <engine>
        <property name="deploymentExportPath">/tmp/</property>
    </engine>

    <container qualifier="wildfy82-embedded" default="true">
        <configuration>
            <property name="jbossHome">C:/rearch/wildfly-8.2.0.Final</property>
            <property name="outputToConsole">true</property>
            <property name="modulePath">C:/rearch/wildfly-8.2.0.Final/modules</property>
            <property name="username">user</property>
            <property name="password">password</property>
        </configuration>
    </container>

    <extension qualifier="spring-deployer">
        <property name="springVersion">4.1.4.RELEASE</property>

        <!-- The version of CGLIB artifact - required by Java-based config, optional, default is 2.2.2 -->
        <property name="cglibVersion">2.2</property>
        <property name="autoPackage">true</property>

    </extension>

</arquillian>

然后我的测试类看起来如下

@RunWith(Arquillian.class)
@SpringAnnotationConfiguration(packages = "com.tb.ao")
@Slf4j
public class ModelServiceTest {

    @Autowired
    private ModelService modelService;

    @Deployment
    public static WebArchive createDeployment() {

        WebArchive war =  ShrinkWrap.create(WebArchive.class, "ao.war")
                .addPackages(true,"com.tb.ao");

        String[] extensions = {".properties"};
        FileUtils.listFiles(new File("src/main/resources"),extensions,false).stream().forEach(file -> war.addAsManifestResource(file));
        return war;

    }

    @Test
    //sometest

    }

最后,我得到如下例外(由于未启动WildFly环境,因此未检测到数据源)

Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:95) [spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    at org.jboss.arquillian.spring.integration.javaconfig.utils.AnnotationApplicationContextProducer.createAnnotatedApplicationContext(AnnotationApplicationContextProducer.java:72) [arquillian-service-integration-spring-javaconfig-1.0.0.Final-20140618.175956-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.spring.integration.javaconfig.utils.AnnotationApplicationContextProducer.createAnnotatedApplicationContext(AnnotationApplicationContextProducer.java:43) [arquillian-service-integration-spring-javaconfig-1.0.0.Final-20140618.175956-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.spring.integration.javaconfig.container.AnnotationRemoteApplicationContextProducer.getApplicationContext(AnnotationRemoteApplicationContextProducer.java:99) [arquillian-service-integration-spring-javaconfig-1.0.0.Final-20140618.175956-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.spring.integration.javaconfig.container.AnnotationRemoteApplicationContextProducer.createApplicationContext(AnnotationRemoteApplicationContextProducer.java:67) [arquillian-service-integration-spring-javaconfig-1.0.0.Final-20140618.175956-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.spring.integration.javaconfig.container.AnnotationRemoteApplicationContextProducer.createApplicationContext(AnnotationRemoteApplicationContextProducer.java:1) [arquillian-service-integration-spring-javaconfig-1.0.0.Final-20140618.175956-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.container.spring.embedded.SpringEmbeddedApplicationContextLifeCycleHandler.createApplicationContext(SpringEmbeddedApplicationContextLifeCycleHandler.java:180) [arquillian-service-container-spring-1.0.0.Final-20140618.175856-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.container.spring.embedded.SpringEmbeddedApplicationContextLifeCycleHandler.createTestApplicationContext(SpringEmbeddedApplicationContextLifeCycleHandler.java:150) [arquillian-service-container-spring-1.0.0.Final-20140618.175856-2.jar:1.0.0.Final-SNAPSHOT]
    at org.jboss.arquillian.container.spring.embedded.SpringEmbeddedApplicationContextLifeCycleHandler.beforeClass(SpringEmbeddedApplicationContextLifeCycleHandler.java:88) [arquillian-service-container-spring-1.0.0.Final-20140618.175856-2.jar:1.0.0.Final-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92) [arquillian-test-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73) [arquillian-test-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116) [arquillian-core-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:87) [arquillian-test-impl-base-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:201) [arquillian-junit-core-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422) [arquillian-junit-core-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54) [arquillian-junit-core-1.1.7.Final.jar:1.1.7.Final]
    at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218) [arquillian-junit-core-1.1.7.Final.jar:1.1.7.Final]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166) [arquillian-junit-core-1.1.7.Final.jar:1.1.7.Final]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) [junit-rt.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130) [idea_rt.jar:na]

1 个答案:

答案 0 :(得分:0)

你有属性

-Xms

-Xmx文件中进行测试?

此外,您需要依赖spring.datasource.url = … spring.datasource.driverClassName = … 数据库驱动程序,例如(对于h2)

application.properties

或者您需要将新模块添加到您的野生动物中并在pom.xml中进行描述。