[英]Error when running a GWTTestCase using maven gwt plugin
我已經創建了一個擴展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>
任何人都可以給我一兩個關於我做錯的提示嗎?
重現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>
我認為正確的做法只是將測試排除在你的maven生命周期之外。 寫這些是什么意思? 你要做的是正確配置maven-surefire-plugin以使其工作。
你看,該插件使用系統類加載器來查找類,但GWTTestCase需要一個URLClassLoader 。 這就是你獲得[WARN] No source path entries; expect subsequent failures
[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所需的類。 您可能需要以不同方式配置它。
問題是測試是通過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測試,但這是另一個問題並且還有另一個問題。 我認為這個問題已經解決。
首先從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測試用例。
對此的解決方案
"[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
我非常有信心這個錯誤與maven設置無關。 我的第一個猜測是測試不在gwt編譯路徑上...我猜有問題的源代碼是:
<source path="com.myproject.test.ui" />
嘗試改為:
<source path="com/myproject/test/ui" />
或者任何適當的路徑。
這個sunfire配置對我有用 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.