只需开始一个新的Gradle项目。
此测试通过:
def 'Launcher.main should call App.launch'(){
given:
GroovyMock(Application, global: true)
when:
Launcher.main()
then:
1 * Application.launch( App, null ) >> null
}
...直到要使用(Java)Mock进行另一个测试,我必须添加以下依赖项:
testImplementation 'net.bytebuddy:byte-buddy:1.10.8'
testImplementation 'org.objenesis:objenesis:3.1'
(注意,我认为这些版本对于Groovy 3. +来说是可以的,我现在正在使用...这两个都是Maven Repo上最新的版本。]]
具有这些依赖性,以上测试失败:
java.lang.InstantiationError: javafx.application.Application at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48) at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73) at org.objenesis.ObjenesisHelper.newInstance(ObjenesisHelper.java:44) at org.spockframework.mock.runtime.MockInstantiator$ObjenesisInstantiator.instantiate(MockInstantiator.java:45) at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:31) at org.spockframework.mock.runtime.GroovyMockFactory.create(GroovyMockFactory.java:57) at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:42) at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47) at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:298) at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:288) at org.spockframework.lang.SpecInternals.GroovyMockImpl(SpecInternals.java:215) at core.AppSpec.Launcher.main should call App.launch(first_tests.groovy:30)
我承认,我对“ bytebuddy”和“ objenesis”实际上只具有最粗略的概念,尽管我认为它是非常聪明的。编辑:刚刚访问了各自的主页,我的概念现在略微不那么粗略了,是的,它非常聪明。
如果无法使用正统的解决方案,是否有可能关闭单个功能(例如测试)对这些依赖项的使用?可能使用一些注释?
编辑
这是MCVE:规格:Java 11.0.5,OS Linux Mint 18.3]
build.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:3.+' testImplementation 'junit:junit:4.12' testImplementation 'org.spockframework:spock-core:2.0-M2-groovy-3.0' testImplementation 'net.bytebuddy:byte-buddy:1.10.8' testImplementation 'org.objenesis:objenesis:3.1' // in light of kriegaex's comments: implementation group: 'cglib', name: 'cglib', version: '3.3.0' } test { useJUnitPlatform() } application { mainClassName = 'core.Launcher' } installDist{}
main.groovy:
class Launcher { static void main(String[] args) { Application.launch(App, null ) } } class App extends Application { void start(Stage primaryStage) { } }
first_tests.groovy:
class AppSpec extends Specification { def 'Launcher.main should call App.launch'(){ given: GroovyMock(Application, global: true) when: Launcher.main() then: 1 * Application.launch( App, null ) >> null } }
[这个项目之所以需要一些东西来调用
Application
子类的原因,在here中进行了解释:它是为了使installDist
可以捆绑在JavaFX中。
只需启动一个新的Gradle项目。该测试通过:def'Launcher.main应该调用App.launch'(){给定:GroovyMock(Application,global:true)当:Launcher.main()然后:...
我们不必使用全局GroovyMock吗?