Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gwt 如何将maven和jUnit测试结合使用?_Gwt_Maven_Junit - Fatal编程技术网

Gwt 如何将maven和jUnit测试结合使用?

Gwt 如何将maven和jUnit测试结合使用?,gwt,maven,junit,Gwt,Maven,Junit,我在Maven中构建了一个gwt应用程序,现在我尝试运行一个简单的gwt测试,如bellow: 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

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

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);
    }
}
在pom.xml文件中,将gwt maven插件和maven surefire插件配置为以下内容:

  <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>
问题2:

我不明白这一点,gwt测试是一个非常简单的测试,为什么它要验证一个不相关的*test.java并搜索它的源代码。虽然最终在通过测试的情况下构建成功,但我如何才能消除这些讨厌的错误消息呢


也许我应该忘记GWTMavin插件,继续使用经典的JUnit测试

我可以帮助回答问题1。要跳过正在运行的测试,请执行以下操作:

mvn (goals) -Dmaven.test.skip=true
这将使它忽略JUnit测试


至于问题2,经典的JUnit可能是一条出路。我不太熟悉GWT,但它是否有测试注释(@test)?第42行出现了什么错误?

您应该像这样将src文件添加到类路径中:

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
  <additionalClasspathElements>
    <additionalClasspathElement>
       ${project.build.sourceDirectory}
    </additionalClasspathElement>
    <additionalClasspathElement>
       ${project.build.testSourceDirectory}
    </additionalClasspathElement>
  </additionalClasspathElements>
  <useManifestOnlyJar>false</useManifestOnlyJar>
  <forkMode>always</forkMode>
  <systemProperties>
    <property>
      <name>gwt.args</name>
      <value>-out \${webAppDirectory}</value>
    </property>
  </systemProperties>
</configuration>


对你来说,在我参与的大多数非琐碎的项目中,对JUnit有一种极端的偏见,原因有两个。首先,与JUnit测试相比,GWTUnit需要对javascript上下文进行加速,这非常缓慢。第二,您正在编写的代码不应该太多,需要基于您的设计和代码结构测试Javascript上下文;主要是因为问题1,但也要记住,您现在正在使用GWT视图编写一些东西,但您确实希望它尽可能不受视图技术的影响。这样,当Swing成功回归卓越地位时,您可以更轻松地将其移植过来。我在开玩笑,更可能的情况是移植到Android,或者谁知道将来还会发生什么

因此,基本上,GWTUnit运行速度非常慢,因为它必须启动环境,而且在所有现实中,您应该(IMHO)分解业务逻辑,这样您就不会永远与任何一种视图渲染技术耦合。在我看来,大多数可以从小部件和面板的纯UI世界中分离出来的逻辑应该是,无论是控制流类型逻辑、验证逻辑,还是其他任何逻辑,而不仅仅是将内容放在屏幕上并将值输入和输出它们。在一个更大的项目中,其他一切都应该从这些类中删除,并将对象作为与GWT没有任何联系的输入。如果您这样做,您就可以JUnit应用程序的绝大多数,速度快得离谱,而且您不必永远等待(在一个大型项目中,GWTUnit的大量覆盖可能需要几个小时,甚至几天)。我不会继续谈论维护一个强大的单元测试覆盖的代码库有多好


这个rant是由我在当前工作中完成的最后一个任务预测的,有一组业务逻辑与视图层一起结束,视图层有178个不同的场景(在展开和构建完整的单元测试套件时),所有这些场景都必须正确执行。这是一个永远都不应该知道它在GWT中运行的东西,但是它被剪短了,并且充满了对视图技术类的引用。事实上,这是为早期版本的软件编写的,没有太大变化,但他们将逻辑封装在视图层(即Swing)中。新团队刚刚重复了早期团队的错误,因为这段代码需要几个小时的手工测试,我确信这并没有100%涵盖场景,与运行它们的几秒钟快速单元测试相反。

嗨,我了解您的问题,可能的解决方案在gwt maven插件文档页面上:

根据插件文档:

有意: “测试”目标受默认集成测试阶段的约束,GWTTestCase不被视为单元测试,因为它们需要整个GWT模块运行

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

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

区分经典测试和GWT测试的一种更简单的方法是将最新的GwtTest命名为“Something.java”。当surefire查找名为“Test.java”的测试时,默认情况下,它们将在测试阶段被忽略

默认情况下,gwt maven插件使用GwtTest*.java作为包含模式,因此此类测试将与标准的Surefire模式不匹配。使用此约定,您不必更改配置

mvn (goals) -Dmaven.test.skip=true
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
  <additionalClasspathElements>
    <additionalClasspathElement>
       ${project.build.sourceDirectory}
    </additionalClasspathElement>
    <additionalClasspathElement>
       ${project.build.testSourceDirectory}
    </additionalClasspathElement>
  </additionalClasspathElements>
  <useManifestOnlyJar>false</useManifestOnlyJar>
  <forkMode>always</forkMode>
  <systemProperties>
    <property>
      <name>gwt.args</name>
      <value>-out \${webAppDirectory}</value>
    </property>
  </systemProperties>
</configuration>