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

Java 使用maven gwt插件运行GWTTestCase时出错,java,maven-2,gwt,unit-testing,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 t

我已经创建了一个扩展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!
下面是我尝试测试的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>


有人能给我一两个提示,说明我做错了什么吗?

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

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

尝试更改为:

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


或者任何合适的路径。

问题在于测试是由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> 

org.apache.maven.plugins
maven surefire插件
**/*GwtTest*.java
**/*Gwt*套件*.java

我仍然无法运行GWTTestCase测试,但这是另一个问题,也是另一个问题的主题。我认为这个问题已经解决了。p> 我认为将测试从maven生命周期中排除是不对的。写下来有什么意义?您需要做的是正确配置maven surefire插件,以使其正常工作

你看,该插件使用系统类加载器来查找类,但是gwtsetcase需要URLClassLoader。这就是您没有获得任何源路径条目的原因;预计会出现后续故障。以及下面的
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>

org.apache.maven.plugins
maven surefire插件
假的
${basedir}/src/main/java
${basedir}/src/test/java
集成测试
测试
请注意
false
条目。
另外,请注意,我添加了测试源和主目录,以允许GWT找到生成Javascript所需的类。您可能需要进行不同的配置。

对我有效。

从中复制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>

maven surefire插件
2.6
${project.build.sourceDirectory}
${project.build.testSourceDirectory}
假的
总是
gwt.args
-out\${webAppDirectory}

首先从
maven surefire插件中排除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>
            <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测试用例

"[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
战争{
//排除不必要的GWT编译器工件
排除“**/gwt unitCache/**”
}

任务widgetset没有POM内容等。很难找出问题所在…在单独的过程中运行测试请将答案标记为正确答案,这样问题就可以结束。我从surefire插件中排除了测试类别,因为我没有使用surefire插件来运行测试。我正在使用Maven Gwt插件。我试图在某个时候使用surefire,但没有成功。也许我会再次尝试使用surefire,但目前我仍使用当前的配置。谢谢您的回答。此配置将创建一个名为
\${webAppDirectory}
的文件夹。但是如果我把它换成别的东西就不行了。我不认为这对我有用。我已经完成了JUnit和Gwt单元测试,这就解决了问题。
ant.java(classname: 'com.google.gwt.dev.Compiler', failOnError: 'yes',  maxmemory: '1000m', fork: 'true')
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