2012-08-06 105 views
2

我正在開發一款需要運行在Glassfish 3.1上的MDB的應用程序。我已經管理過使用嵌入式容器運行簡單EJB的單元/集成測試,沒有任何問題。現在我正在嘗試爲我的MDB創建集成測試。JMS與Maven和Glassfish的集成測試

1)我嘗試以編程方式啓動Glassfish嵌入式服務器,但它不支持創建JMS隊列。

2)我從Maven插件運行Glassfish服務器。 現在我可以創建隊列,並部署我的MDB,完全沒有問題。現在,我無法弄清楚運行JUnit的方式。
- 當我創建InitialContext時,訪問本地服務器時超時。我沒有辦法訪問我的豆子。

我找到了一個解決辦法,但它不是完美的服務我的需求:
在我的測試源,我創建了一個簡單的辛格爾頓@Startup豆。在@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> 

現在,有沒有什麼辦法我IntegrationTest可以使用surfire推出,以產生一個適當的報告,並不能建立,如果測試別通過?更不用說Cobertura了。

謝謝你的幫助。

回答

2

我沒有解決我的問題可以這麼說。我所要做的就是升級到GlassFish 3.1.1。該版本支持嵌入模式下的JMS。因此,我可以以編程方式運行服務器,並使用admin命令運行程序部署我想要的隊列。

我只是失去了一些時間試圖名字我的連接工廠JMS/connectionFactory的其中JMS/前綴是不必要的,是失敗的所有查詢。

我還必須在所有單元測試中包含線程睡眠,否則服務器在測試結束之前會關閉。