使用maven gwt插件运行GWTTestCase时出错

时间:2010-04-29 12:18:17

标签: java maven-2 gwt unit-testing

我已经创建了一个扩展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>

任何人都可以给我一两个关于我做错的提示吗?

7 个答案:

答案 0 :(得分:9)

重现KevinWong从maven-gwt-plugin doc使用的解决方案,在尝试其他解决方案失去一个多小时后,这对我有用。

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

答案 1 :(得分:6)

我认为正确的做法只是将测试排除在你的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>

请注意<userSystemClassLoader>false</useSystemClassLoader>条目。 另外,请注意我添加了测试和主目录的源代码,以便允许GWT找到生成Javascript所需的类。您可能需要以不同方式配置它。

答案 2 :(得分:5)

问题是测试是由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测试,但这是另一个问题,并且还有另一个问题。我认为这个问题已经解决。

答案 3 :(得分:3)

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

        <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测试用例。

答案 4 :(得分:0)

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

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

尝试改为:

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

或任何适当的路径。

答案 5 :(得分:0)

This sunfire config为我工作。

答案 6 :(得分:0)

解决方案

"[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