这是一个 MCVE:
main.groovy:
package core
import javafx.application.Application
import javafx.stage.Stage
import javafx.fxml.FXMLLoader
class App extends Application {
FXMLLoader fxmlLoader = new FXMLLoader()
static App instance
FXMLController fxmlController
void start(Stage primaryStage) {
instance = this
fxmlLoader.load( getClass().getResource("mainWindow.fxml").openStream() )
primaryStage.show()
}
}
class FXMLController {}
testfx.groovy:
package core
import javafx.fxml.FXMLLoader
import javafx.scene.Node
import javafx.stage.Stage
import org.testfx.framework.spock.ApplicationSpec
class FirstFXSpec extends ApplicationSpec {
void start(Stage stage) { }
def 'at start, fxmlLoader should load mainWindow.fxml'() {
given:
App.instance = new App()
App.instance.fxmlLoader = Mock(FXMLLoader) {
getController() >> Mock(FXMLController)
}
Node mockNode = Mock(Node)
// Stage mockStage = GroovyMock( Stage ){
Stage mockStage = Mock( Stage ){
show() >> null
}
when:
App.instance.start( mockStage )
then:
true // details of test omitted
}
}
构建.gradle:
plugins {
id 'groovy'
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories { mavenCentral() }
javafx {
version = '11.0.2'
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
dependencies {
implementation 'org.codehaus.groovy:groovy:2.5.+'
testImplementation 'junit:junit:4.12'
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
testImplementation 'net.bytebuddy:byte-buddy:1.9.3'
testImplementation 'org.objenesis:objenesis:2.6'
testImplementation 'org.testfx:testfx-spock:4.0.15-alpha'
}
application {
mainClassName = 'core.App'
}
文件 src/main/resources/core/mainWindow.fxml 的详细信息并不重要,但这里有一个示例:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="core.FXMLController" >
<children>
<Label text="Some label"></Label>
</children>
</VBox>
上述测试失败。真正的方法Stage.show()
被调用,它是一个模拟的事实被忽略了。
如果我将其从 a 更改为Mock
a ,则使用GroovyMock
模拟方法show()
并且测试通过。
为什么普通的SpockMock
会被忽略?