0

我正在尝试在离线机器上使用 gradle 运行 TestFX 测试。

使用类似的命令

gradle -Dtest.single=ViewsTest test

在一台机器上它运行良好,但在另一台机器上它失败了

NoClassDefFoundError: org.testfx.api.FxRobotException

这可能意味着这里的双重错误。运行测试时会发生一个(未知)错误,而在TestFX尝试生成有关它的异常时会发生另一个错误。

我对后一个感兴趣:为什么 Gradle 看不到这个类?

同时,我编写了测试以检查课程是否可用

 @Test
   public void causes_exception_on_unknown_button() {
      try {
         point(new Predicate<Node>() {
            @Override
            public boolean apply(Node input) {
               return false;
            }
         });
      }
      catch (FxRobotException e) {
         System.out.println("Expected exception found");
         return;
      }
      throw new AssertionError("No expected exception found");
   }

它通过了,意味着课程FxRobotException可用。

确认,以下依赖项在build.gradle

testCompile group: 'org.testfx', name: 'testfx-junit', version: '4.0.4-alpha'
testCompile group: 'org.testfx', name: 'testfx-core', version: '4.0.4-alpha'

怎么可能?

更新

如果我跑

gradle -Dtest.single=ViewsTest test -i

(更多信息)

发生以下异常:

 java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org.springframework.beans.F
atalBeanException

        Caused by:
        java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException

            Caused by:
            java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException

    org.testfx.api.FxRobotException: the query "#menuFile" returned no nodes.

奇怪的是,现在我们确实看到了来自的实际消息,FxRobotException并且 class-def 错误现在与不同的FatalBeanException. 同时,根本原因似乎是某处的执行异常。

更新 2

build.gradle如下(注意,它是“本地的”):

buildscript {
    repositories {
        mavenLocal()
    }

    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"
        classpath 'org.ajoberstar:gradle-git:1.5.1'
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'antlr'

ext {

    try {
        revision = org.ajoberstar.grgit.Grgit.open().head().abbreviatedId
    }
    catch(all) {
        revision = 'SNAPSHOT'
    }
}


project.group = 'com.cireca'
version = '0.1.20.' + getDate() + '-' + revision

description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8





repositories {
    mavenLocal()
    flatDir {
        dirs 'lib'
    }

}

dependencies {
    ext.springVersion = '4.2.6.RELEASE'
    ext.junitVersion = '4.12'


    antlr "org.antlr:antlr4:4.5" // use ANTLR version 4

    compile("org.hibernate:hibernate-entitymanager:5.1.0.Final")
    compile("com.opencsv:opencsv:3.7")
    compile("com.cireca.overlaywidget:OverlayWidget:1.0-SNAPSHOT")
    compile("org.springframework:spring-context:$springVersion")
    compile("org.springframework:spring-beans:$springVersion")
    compile("org.springframework:spring-jdbc:$springVersion")
    compile("org.springframework:spring-orm:$springVersion")
    compile('mysql:mysql-connector-java:5.1.38')
    compile('org.apache.commons:commons-dbcp2:2.1.1')
    compile('org.controlsfx:controlsfx:8.40.12')
    compile('org.apache.commons:commons-io:1.3.2')
    compile('org.apache.commons:commons-csv:1.2')
    compile('org.apache.commons:commons-collections4:4.1')
    compile('org.apache.commons:commons-lang3:3.4')
    compile('commons-beanutils:commons-beanutils:1.9.2')
    compile('com.google.guava:guava:19.0')
    compile('org.cache2k:cache2k-core:0.25-BETA')
    compile('it.unimi.dsi:fastutil:7.0.12')
    compile('org.jadira.usertype:usertype.core:5.0.0.GA')
    compile('joda-time:joda-time:2.9.2')
    compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.22'
    compile group: 'org.fxmisc.easybind', name: 'easybind', version: '1.0.3'
    compile group: 'io.reactivex', name: 'rxjavafx', version: '2.0.2'
    compile name: 'orsoncharts-1.5'
    compile name: 'fxgraphics2d-1.3'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
    compile group: 'org.eclipse.swt', name: 'org.eclipse.swt.win32.win32.x86', version: '4.6.1'
    compile group: 'org.eclipse.swt', name: 'org.eclipse.swt.win32.win32.x86_64', version: '4.6.1'

    testCompile("org.springframework:spring-test:$springVersion")
    testCompile("junit:junit:$junitVersion")
    testCompile('org.assertj:assertj-core:3.5.2')
    testCompile group: 'org.testfx', name: 'testfx-junit', version: '4.0.4-alpha'
    testCompile group: 'org.testfx', name: 'testfx-core', version: '4.0.4-alpha'


}

sourceSets {
    demo
    attempts {
        compileClasspath += main.output
        runtimeClasspath += main.output
    }
    tools
    antlrGenerated {
        java {

        }
    }
}

jar {
    manifest {
        attributes 'Main-Class': 'com.cireca.Main'
    }

}


attemptsClasses.dependsOn build

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}


generateGrammarSource {
    maxHeapSize = "64m"
    arguments += ["-visitor", "-long-messages"]
}

task packageTests(type: Jar) {
    from sourceSets.test.output
}

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmm')
    return formattedDate
}

task zip4BuildServer(type: Zip) {
    classifier = 'src'
    from projectDir
    include 'src/**/*'
    include 'lib/**/*'
    include 'gradle/**/*'
    include 'build.gradle'
    include 'settings.gradle'
    include 'data_source.xml'
    include 'gradlew'
    include 'gradlew.bat'
    archiveName "${baseName}-${getDate()}-${revision}.${extension}"
}

test {
    testLogging {
        // Show that tests are run in the command-line output
        events 'started', 'passed'
    }
}

并且错误跟踪如下:

java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
    at org.testfx.util.WaitForAsyncUtils.waitFor(WaitForAsyncUtils.java:161)
    at org.testfx.api.FxToolkit.waitForSetup(FxToolkit.java:269)
    at org.testfx.api.FxToolkit.setupApplication(FxToolkit.java:175)
    at org.testfx.framework.junit.ApplicationTest.internalBefore(ApplicationTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
    at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:476)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:455)
    at com.google.common.util.concurrent.AbstractFuture$TrustedFuture.get(AbstractFuture.java:79)
    at org.testfx.toolkit.impl.ToolkitServiceImpl.lambda$setupApplication$60(ToolkitServiceImpl.java:144)
    at org.testfx.util.WaitForAsyncUtils.callCallableAndSetFuture(WaitForAsyncUtils.java:363)
    at org.testfx.util.WaitForAsyncUtils.lambda$async$8(WaitForAsyncUtils.java:88)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    ... 3 more
Caused by: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at com.cireca.filterwidget.javafx.spring.SpringFX.initializeSpringContext(SpringFX.java:65)
    at com.cireca.filterwidget.javafx.spring.SpringFX.initialize(SpringFX.java:123)
    at com.cireca.filterwidget.javafx.spring.SpringFX.init(SpringFX.java:73)
    at com.cireca.testfx.TestFXUtils.startLDT(TestFXUtils.java:38)
    at com.cireca.testfx.MainUITest.start(MainUITest.java:37)
    at org.testfx.framework.junit.ApplicationAdapter.start(ApplicationAdapter.java:54)
    at org.testfx.toolkit.impl.ApplicationServiceImpl.lambda$start$61(ApplicationServiceImpl.java:62)
    at org.testfx.util.WaitForAsyncUtils.callCallableAndSetFuture(WaitForAsyncUtils.java:363)
    at org.testfx.util.WaitForAsyncUtils.lambda$asyncFx$9(WaitForAsyncUtils.java:115)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more

更新 3

依赖检查结果

>gradle dependencyInsight --dependency spring-beans --configuration testRuntime
:dependencyInsight
org.springframework:spring-beans:4.2.6.RELEASE
+--- testRuntime
+--- org.springframework:spring-aop:4.2.6.RELEASE
|    \--- org.springframework:spring-context:4.2.6.RELEASE
|         \--- testRuntime
+--- org.springframework:spring-context:4.2.6.RELEASE (*)
+--- org.springframework:spring-jdbc:4.2.6.RELEASE
|    +--- testRuntime
|    \--- org.springframework:spring-orm:4.2.6.RELEASE
|         \--- testRuntime
+--- org.springframework:spring-orm:4.2.6.RELEASE (*)
\--- org.springframework:spring-tx:4.2.6.RELEASE
     +--- org.springframework:spring-jdbc:4.2.6.RELEASE (*)
     \--- org.springframework:spring-orm:4.2.6.RELEASE (*)

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 5.694 secs


>gradle dependencyInsight --dependency spring-core --configuration testRuntime
:dependencyInsight
org.springframework:spring-core:4.2.6.RELEASE
+--- org.springframework:spring-aop:4.2.6.RELEASE
|    \--- org.springframework:spring-context:4.2.6.RELEASE
|         \--- testRuntime
+--- org.springframework:spring-beans:4.2.6.RELEASE
|    +--- testRuntime
|    +--- org.springframework:spring-context:4.2.6.RELEASE (*)
|    +--- org.springframework:spring-jdbc:4.2.6.RELEASE
|    |    +--- testRuntime
|    |    \--- org.springframework:spring-orm:4.2.6.RELEASE
|    |         \--- testRuntime
|    +--- org.springframework:spring-orm:4.2.6.RELEASE (*)
|    +--- org.springframework:spring-aop:4.2.6.RELEASE (*)
|    \--- org.springframework:spring-tx:4.2.6.RELEASE
|         +--- org.springframework:spring-jdbc:4.2.6.RELEASE (*)
|         \--- org.springframework:spring-orm:4.2.6.RELEASE (*)
+--- org.springframework:spring-context:4.2.6.RELEASE (*)
+--- org.springframework:spring-expression:4.2.6.RELEASE
|    \--- org.springframework:spring-context:4.2.6.RELEASE (*)
+--- org.springframework:spring-jdbc:4.2.6.RELEASE (*)
+--- org.springframework:spring-orm:4.2.6.RELEASE (*)
+--- org.springframework:spring-test:4.2.6.RELEASE
|    \--- testRuntime
\--- org.springframework:spring-tx:4.2.6.RELEASE (*)

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 5.557 secs
4

2 回答 2

1

确保testfx-junitViewsTest由同一个类加载器加载。

可能的原因:

  • jar 包含ViewsTest.class%JAVA_HOME%\jre\lib
  • 包含的项目ViewsTest.class被添加到buildscript { classpath ... } }gradle中的块
于 2017-03-27T12:14:59.590 回答
0

在 Maven Central 上进行快速搜索,我发现它org.springframework.beans.FatalBeanExceptionspring-beansspring-core依赖项中。

您可能希望使用dependencyInsight来查看这两个依赖项发生了什么。

例如:在命令行输入以下内容

gradle dependencyInsight --dependency spring-beans --configuration testRuntime
gradle dependencyInsight --dependency spring-core --configuration testRuntime

我怀疑在您的类路径的某个地方,您的一个依赖项需要某个版本,spring-beans或者spring-core但 Gradle 正在使用另一个版本解决

于 2017-03-29T09:59:34.170 回答