【问题标题】:Error when running a GWTTestCase using maven gwt plugin使用 maven gwt 插件运行 GWTTestCase 时出错
【发布时间】:2011-02-13 19:06:30
【问题描述】:

我创建了一个扩展 GWTTestCase 的测试,但出现此错误:

mvn integration-test gwt:test
...
Running com.myproject.test.ui.GwtTestMyFirstTestCase
Translatable source found in...                       
[WARN] No source path entries; expect subsequent failures
[ERROR] Unable to find type 'java.lang.Object'
[ERROR] Hint: Check that your module inherits 'com.google.gwt.core.Core' either directly or indirectly (most often by inheriting module 'com.google.gwt.user.User')
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.1 sec <<< FAILURE!

GwtTestMyFirstTestCase.java 位于 /src/test/java,而 GWT 模块位于 src/main/java。我认为这应该不是问题。

我已经按照http://mojo.codehaus.org/gwt-maven-plugin/user-guide/testing.html 完成了所有要求,当然我的 gwt 模块已经间接导入了 com.google.gwt.core.Core。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>main</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Main Module</name>

<properties>
    <gwt.module>com.myproject.MainModule</gwt.module>
</properties>

<parent>
    <groupId>com.myproject</groupId>
    <artifactId>app</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<dependencies>

    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>app-commons</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-dev</artifactId>
        <version>${gwt.version}</version>
        <scope>provided</scope>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
                <outputFile>../app/src/main/webapp/WEB-INF/main.tree</outputFile>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>


            <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
            </executions>

        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <classesDirectory>
                    ${project.build.directory}/${project.build.finalName}/${gwt.module}
                </classesDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

这里是测试用例,位于/src/test/java/com/myproject/test/ui

public class GwtTestMyFirstTestCase extends GWTTestCase {

    @Override
    public String getModuleName() {
        return "com.myproject.MainModule";
    }

    public void testSomething() {


    }

}

这是我要测试的 gwt 模块,位于 src/main/java/com/myproject/MainModule.gwt.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.1/distro-source/core/src/gwt-module.dtd">
<module>

    <inherits name='com.myproject.Commons' />

    <source path="site" />

    <source path="com.myproject.test.ui" />

    <set-property name="gwt.suppressNonStaticFinalFieldWarnings" value="true" />

    <entry-point class='com.myproject.site.SiteModuleEntry' />
</module>

谁能给我一两个关于我做错了什么的提示?

【问题讨论】:

  • 如果没有 POM 内容等,很难说明问题是什么......
  • 在单独的进程中运行测试See my answer

标签: java maven-2 gwt unit-testing


【解决方案1】:

This sunfire config 为我工作。

【讨论】:

    【解决方案2】:

    解决办法

    "[ERROR] Unable to find type 'java.lang.Object'
    [ant:java]       [ERROR] Hint: Check that your module inherits 'com.google.gwt.core.Core' 
    either directly or indirectly (most often by inheriting module 'com.google.gwt.user.User')"  
    

    GWT 编译错误是调用 GWT 编译器时使用“fork='true'”。

    这就是为什么这里发布的解决方案神奇地起作用的原因——它们有“forkMode=always”和类似的。

    我是这样称呼 GWT 编译器的:

    ant.java(classname: 'com.google.gwt.dev.Compiler', failOnError: 'yes',  maxmemory: '1000m', fork: 'true')
    

    这是 Gradle 中完整的 GWT 编译器调用:

    war {
        // Exclude unneccessery GWT Compiler artifacts
        exclude "**/gwt-unitCache/**"
    }
    
    task widgetset << {
        // Create widgetset directory (if needed)
        def created = (new File(gwtBuildDir)).mkdirs()
    
        // Compile
        ant.java(classname: 'com.google.gwt.dev.Compiler', failOnError: 'yes',  maxmemory: '1000m', fork: 'true')
                {
                    classpath {
                        pathElement(path: configurations.compile.asPath)
                        pathElement(path: sourceSets.main.runtimeClasspath.asPath)
                        sourceSets.main.java.srcDirs.each {
                            pathelement(location: it.absolutePath)
                        }
                    }
    
                    arg(line: '-war ' + gwtBuildDir)
                    arg(line: '-logLevel INFO')
                    arg(line: '-style OBF')
                    arg(line: '-localWorkers 2')
                    arg(line: widgetsetClass)
    
    //                jvmarg(value: '-Djava.awt.headless=true')
    //                jvmarg(value: '-XX:MaxPermSize=256M')
    //                jvmarg(value: '-Xmx500M')
                }
    }
    
    
    // Require widgetset compilation before WAR is built
    war.dependsOn widgetset
    

    【讨论】:

      【解决方案3】:

      首先从maven-surefire-plugin中排除gwt测试用例:

              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.12.4</version>
                  <configuration>
                      <excludes>
                          <exclude>**/*GwtTest.java</exclude>
                      </excludes>
                  </configuration>
              </plugin>
      

      然后配置gwt-maven-plugin:

                  <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>gwt-maven-plugin</artifactId>
                  <version>2.5.0</version>
                  <executions>
                      <execution>
                          <goals>
                              <goal>test</goal>
                          </goals>
                      </execution>
                  </executions>
                  <configuration>
                                          <includes>**/*GwtTest.java</includes>
                                          <mode>htmlunit</mode>
                  </configuration>
              </plugin>
      

      现在您可以使用 gwt:test 轻松运行 gwt 测试用例。

      【讨论】:

      • 这对我有用。我有 JUnit 和 Gwt 单元测试,这解决了。
      【解决方案4】:

      maven-gwt-plugin doc 复制 KevinWong 使用的解决方案,在尝试其他解决方案一个多小时后,它对我有用。

      <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>
        </plugin>
      

      【讨论】:

      • 此配置会创建一个名为 \${webAppDirectory} 的文件夹。但是,如果我将其更改为其他任何内容,它就不起作用。我什至没有
      【解决方案5】:

      我认为从你的 Maven 生命周期中排除测试是不正确的。写它们有什么意义?您需要做的是正确配置 maven-surefire-plugin 以使其正常工作。

      您知道,该插件使用系统类加载器来查找类,但 GWTTestCase 需要一个 URLClassLoader。这就是您收到[WARN] No source path entries; expect subsequent failures 的原因。和以下ClassNotFoundException。不过不用担心。告诉 maven 使用 URLClassLoader 很容易:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
           <useSystemClassLoader>false</useSystemClassLoader>
           <additionalClasspathElements>
             <additionalClasspathElement>${basedir}/src/main/java</additionalClasspathElement>
             <additionalClasspathElement>${basedir}/src/test/java</additionalClasspathElement>
           </additionalClasspathElements>
        </configuration>
        <executions>
          <execution>
            <phase>integration-test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
         </executions>
      </plugin>
      

      请注意&lt;userSystemClassLoader&gt;false&lt;/useSystemClassLoader&gt; 条目。 另外,请注意,我添加了测试和主目录的源代码,以便让 GWT 找到生成 Javascript 所需的类。您可能需要进行不同的配置。

      【讨论】:

      • 我从surefire插件中排除了测试类,因为我没有使用surefire插件来运行我的测试。我正在为此使用 Maven Gwt 插件。我在某个时候尝试使用surefire,但没有成功。也许我会再次尝试使用surefire,但现在我仍然使用我当前的配置。无论如何感谢您的回答。
      【解决方案6】:

      问题是测试是由surefire而不是gwt-maven插件运行的。我必须从 surefire 插件中明确排除我的 gwt 测试:

      <plugin>
             <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
      
                  <configuration>
                      <excludes>
                          <exclude>**/*GwtTest*.java</exclude>
                          <exclude>**/*Gwt*Suite*.java</exclude>
                      </excludes>
                  </configuration>
      </plugin> 
      

      我仍然无法运行我的 GWTTestCase 测试,但这是另一个问题和另一个问题的主题。我认为这个问题解决了。

      【讨论】:

      • 请把答案标记为正确的,这样问题就可以结束了。
      【解决方案7】:

      我非常有信心这个错误与 maven 设置无关。我的第一个猜测是测试不在 gwt 编译路径上......我猜有问题的源代码是:

      <source path="com.myproject.test.ui" />
      

      尝试更改为:

      <source path="com/myproject/test/ui" />
      

      或任何合适的路径。

      【讨论】:

        猜你喜欢
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 2014-05-19
        • 1970-01-01
        • 2012-12-03
        • 2011-10-04
        • 2014-05-09
        相关资源
        最近更新 更多