2

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

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

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

我找到了一种解决方法,但它不能完美地满足我的需求:
在我的测试源中,我创建了一个简单的 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了。

谢谢您的帮助。

4

1 回答 1

2

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

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

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

于 2012-08-10T08:22:14.287 回答