我如何一起使用maven和jUnit测试?

use*_*587 6 gwt junit maven

我有一个在Maven中构建的gwt应用程序,现在我尝试运行一个简单的GWT测试,如下:

public class GwtTestLaughter extends GWTTestCase {

  /**
   * Specifies a module to use when running this test case. The returned
   * module must include the source for this class.
   * 
   * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
   */
    @Override
    public String getModuleName() {
        return "com.sample.services.joker.laughter.Laughter";
    }

    /**
     * Add as many tests as you like
     */
    public void testSimple() {
        assertTrue(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

在pom.xml文件中,配置gwt-maven-plugin和maven-surefire-plugin如下:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.1.0-1</version>
    <configuration>
      <!-- Use the 'war' directory for GWT hosted mode -->
      <output>${basedir}/war</output>
      <webXml>${basedir}/war/WEB-INF/web.xml</webXml>
      <runTarget>index.html</runTarget>
      <!-- Make sure the GWT compiler uses Xerces -->
  <extraJvmArgs>
    -Dgwt.style=DETAILED -Xmx512M -Xss1024k -XX:MaxPermSize=128m -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl -Dlogback.configurationFile=./src/test/resources/logback-test.xml
  </extraJvmArgs>
</configuration>
<executions>
  <execution>
    <goals>
      <goal>compile</goal>
      <goal>test</goal>
    </goals>
  </execution>
</executions>
Run Code Online (Sandbox Code Playgroud)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <useFile>false</useFile>
       <forkMode>once</forkMode>
       <argLine>-Xmx128m</argLine>
       <systemPropertyVariable>
        <property>
          <name>log4j.configuration</name>
          <value>log4j.properties</value>
        </property>
       </systemPropertyVariables>
    </configuration>
    <executions>
       <execution>
          <id>unit-test</id>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
              <skip>false</skip>
              <includes>
                 <include>**/*Test.java</include>
              <includes>
              <excludes>
                 <exclude>**/GwtTest*.java</exclude>
              </excludes>
          </configuration>
        </execution>
   <execution>
      <id>integration-test</id>
      <phase>integration-test</phase>
      <goals>
         <goal>test</goal>
      </goals>
      <configuration>
          <skip>true</skip>
          <includes>
             <include>**/GwtTest*.java</include>
          <includes>
          <excludes>
             <exclude>**/*Test.java</exclude>
          </excludes>
      </configuration>
    </execution>
    <executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

当我在命令行中运行'mvn test'时,我只能看到正常运行的Junit测试(带有Test.java文件名的测试),当我运行'mvn integration-test'时,我仍然看到所有的测试,包括正常的Junit运行测试和Gwt测试(具有GwtTest .java文件名的测试).

问题1:

如何在集成测试期间完全排除运行正常的Junit测试?或那是不可能的?因为在默认的maven生命周期中,测试阶段被定义为在集成测试之前存在,所以没有办法跳过测试阶段来运行纯粹的集成测试?

由于我混合了/ src/test/java文件夹下的所有测试代码,当我运行'mvn integration-test'并在命令行窗口中观察输出时,我看到以下内容:

[INFO] running com.sample.services.joker.laughter.client.GwtTestLaughter 
..
[INFO] Validating newly compiled units
[INFO]    [ERROR] Errors in 'file:...src/test/java/com/sample/joker/laughter/client/file1Test.java'..
[INFO]    [ERROR] Line 42: No source code is available for type...; did you forget to inherit a required module?
...
Run Code Online (Sandbox Code Playgroud)

问题2:

我不明白这一点,gwt测试是一个非常简单的测试,为什么它会验证一个不相关的*Test.java并搜索它的源代码.虽然最终通过测试成功建立成功,我怎么能摆脱那些讨厌的错误信息?

也许我应该忘记gwt-mavin-plugin并坚持使用经典的Juint测试?

Mru*_*ukh 7

嗨,我理解你的问题,可能的解决方案在于这个gwt-maven-plugin文档页面:https://gwt-maven-plugin.github.io/gwt-maven-plugin/user-guide/testing.html

根据插件文档:

意图:"测试"目标默认绑定到集成测试阶段,GWTTestCase不被视为单元测试,因为它们需要整个GWT模块运行.

要运行基于Surefire和gwt-maven-plugin的测试(使用Surefire进行常规服务器端JUnit测试,以及使用GWT进行客户端模型和控制器测试),您需要将这些测试彼此区分开来.这是使用命名约定完成的.

您可以配置Surefire插件(负责在maven构建期间运行测试)以使用某些命名模式跳过GwtTests.

分离经典和GWT测试的一种更简单的方法是命名最后的GwtTest"Something".java.由于surefire在默认情况下会查找名为Something"Test".java的测试,因此在测试阶段将忽略它们.

默认情况下,gwt-maven-plugin使用GwtTest*.java作为包含模式,以便此类测试与标准Surefire模式不匹配.使用此约定,您不必更改配置.


Ton*_*ony 1

我可以帮助解决问题 1。要跳过运行测试:

mvn (goals) -Dmaven.test.skip=true
Run Code Online (Sandbox Code Playgroud)

这将使其忽略 JUnit 测试。

至于问题 2,经典的 JUnit 可能是最佳选择。我对 GWT 不太熟悉,但是它有测试注释(@test)吗?第 42 行有什么错误?