与Maven和Glassfish进行JMS集成测试

时间:2012-08-06 12:06:48

标签: maven glassfish integration-testing

我正在开发一个需要MDB的应用程序,运行在Glassfish 3.1上。我已经设法使用嵌入式容器运行简单EJB的单元/集成测试,没有问题。现在我正在尝试为我的MDB创建集成测试。

1)我尝试以编程方式启动Glassfish嵌入式服务器,但它不支持创建JMS队列。

2)我从Maven插件运行Glassfish服务器。 现在我可以创建队列,并部署我的MDB,完全没问题。现在,我无法找到一种运行JUnit的方法 - 当我创建InitialContext时,它在访问本地服务器时超时。我无法访问我的豆子。

我找到了一个解决方法,但它不能完美地满足我的需求:
在我的测试源中,我创建了一个简单的Singleton @Startup bean。在@PostConstruct方法中,我调用了我想要实现的单元测试类。为了部署这个bean,我有一个特殊的特殊maven构建规则,它将我的一些测试文件打包在EJB jar中。部署这个特殊的jar会导致我的测试正在启动。为清楚起见,这是我的Maven文件的摘录:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.build.directory}/classes</directory>
                            </resource>
                            <resource>
                                <directory>${project.build.directory}/test-classes</directory>
                                <includes>
                                    <include>**/*TestTrigger.class</include>
                                    <include>**/*IntegrationTest.class</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <ejbVersion>3.1</ejbVersion>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>ejb</goal>
                    </goals>
                    <configuration>
                        <classifier>TEST</classifier>
                        <outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>${glassfish.version}</version>
            <configuration>
                <goalPrefix>glassfish</goalPrefix>
                <app>target/${project.build.finalName}-TEST.jar</app>
                <port>8080</port>
                <name>MyApp</name>
                <serverID>embedded</serverID>
            </configuration>
            <executions>
                <execution>
                    <id>start</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>admin</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>admin</goal>
                    </goals>
                    <configuration>
                        <commands>
                            <param>create-jms-resource --restype javax.jms.QueueConnectionFactory jms/TestQueueConnectionFactory</param>
                            <param>create-jms-resource --restype javax.jms.Queue --property imqDestinationName=ceQueue jms/ceQueue</param>
                        </commands>
                    </configuration>
                </execution>
                <execution>
                    <id>deploy</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>undeploy</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.8.2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>

现在,有没有什么方法可以使用surfire启动我的IntegrationTest,以便在测试未通过时生成正确的报告和失败的构建?更不用说Cobertura。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我没有解决我的问题。我要做的是升级到GlassFish 3.1.1。此版本支持嵌入模式下的JMS。因此,我可以以编程方式运行服务器,并使用admin命令运行程序部署我想要的队列。

我只是失去了一些时间来尝试命名我的连接工厂 jms / connectionFactory ,其中 jms / 前缀是不必要的并且所有查找失败。

我还必须在所有单元测试中包含一个线程休眠,否则服务器会在测试结束前关闭。

相关问题