为什么我的 Spock 测试没有执行并且我在执行时得到零测试结果:
./gradlew clean test
与我的Openjdk 11的TestFX Spock Gradle 项目?
我的 Spock 测试类编译正常但未执行。
这是我的控制台:
Working Directory: /home/~/EclipseProjects/gradleTestfxSpock
Gradle user home: /home/~/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 5.0
Java Home: /usr/lib/jvm/jdk-11.0.2+9
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: clean test
> Configure project :
Found module name 'mtd'
> Task :clean
> Task :compileJava
> Task :compileGroovy NO-SOURCE
> Task :processResources
> Task :classes
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy
> Task :processTestResources
> Task :testClasses
> Task :test
BUILD SUCCESSFUL in 6s
6 actionable tasks: 6 executed
这是我的build.gradle:
plugins {
id 'org.openjfx.javafxplugin' version '0.0.7'
id 'application'
id 'groovy'
}
mainClassName = 'mtd/gradleTestfxSpock.Main'
sourceCompatibility = 11
targetCompatibility = 11
repositories {
jcenter()
}
dependencies {
implementation 'org.testfx:testfx-spock:4.0.15-alpha'
testCompile 'org.testfx:testfx-core:4.0.15-alpha'
testCompile (group: 'org.spockframework', name: 'spock-core', version: '1.3-groovy-2.5')
testCompile ('org.codehaus.groovy:groovy-all:2.5.6')
testRuntime(
'com.athaydes:spock-reports:1.2.7',
'cglib:cglib-nodep:3.2.4'
)
}
javafx {
version = "11.0.2"
modules = [ 'javafx.controls',
'javafx.fxml',
'javafx.web'
]
}
compileJava {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.controls',
'--add-modules', 'javafx.fxml',
'--add-modules', 'javafx.web'
]
}
}
test {
doFirst {
jvmArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-MODULE-PATH',
'--add-exports', 'javafx.graphics/com.sun.javafx.application=org.testfx'
]
}
}
这是我的module-info.java:
module mtd {
requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;
requires javafx.web;
requires org.testfx;
requires testfx.spock;
opens gradleTestfxSpock to javafx.graphics;
exports gradleTestfxSpock;
}
这是我的Spock 测试代码:
package gradleTestfxSpock;
import org.testfx.framework.spock.ApplicationSpec;
import javafx.stage.Stage
public class MainTest extends ApplicationSpec{
def "Main Test 01"() {
expect:
println("you are in Main test 01");
}
@Override
public void start(Stage arg0) throws Exception {
// TODO Auto-generated method stub
}
}
这是我的JavaFX 代码:
package gradleTestfxSpock;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
和控制器:
package gradleTestfxSpock;
public class Controller {
}
这是我的eclipse gradle 项目结构:
在其他 eclipse gradle 项目中,我已经成功执行了没有 Spock的TestFX Junit4测试:
并且分别我已经成功地执行了相同的Spock 测试,没有 TestFX和没有 JUnit:
我确实注意到这个 Spock 测试的一些警告:
Working Directory: /home/~/EclipseProjects/gradleSpock
Gradle user home: /home/~/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 5.0
Java Home: /usr/lib/jvm/jdk-11.0.2+9
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: clean test
> Task :clean
> Task :compileJava
> Task :compileGroovy NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/home/dm/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.6/6936e700f0fb1b50bac0698ada4347a769d40199/groovy-2.5.6.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
BUILD SUCCESSFUL in 9s
4 actionable tasks: 4 executed
结论
如果带有 JUnit 的 TestFX 可以工作,而 Spock 单独可以工作,但是带有 Spock 的 TestFX 不起作用,那么配置是否有问题:
'org.testfx:testfx-spock:4.0.15-alpha'
非常感谢任何想法或帮助。
ps 忘了说我也在Netbeans中创建了TestFX/Spock项目,复制了eclipse项目,得到了同样的结果!
更多测试
不幸的是,下面评论中的伦纳德·布鲁宁斯非常好的建议之后的更多测试组合没有奏效。
我修改后的 module-info.java 看起来像:
module mtd {
requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;
requires javafx.web;
requires org.testfx.junit;
requires org.testfx;
requires testfx.spock;
requires spock.core;
requires junit;
opens gradleTestfxSpock to javafx.graphics, org.testfx, testfx.spock, spock.core, junit, org.testfx.junit;
exports gradleTestfxSpock;
}
我将此添加到我的 gradle.build 依赖项中以防万一:
implementation 'org.testfx:testfx-junit:4.0.15-alpha'
还是不开心...