[英]JUnit tests fail if run as part of a test suite
I have a test class that has several tests and all pass. 我有一个测试类,其中包含多个测试并且全部通过。 If I add the class to a test suite, all tests fail. 如果将类添加到测试套件中,则所有测试都会失败。
The reason for that is that dependencies to JavaFX types cannot be resolved. 其原因是无法解决对JavaFX类型的依赖关系。 I use a test rule that initializes the JavaFX framework (eg the application thread etc.) and this seems to work fine as the passed tests indicate. 我使用了一个初始化JavaFX框架(例如,应用程序线程等)的测试规则 ,这似乎通过了测试表明可以正常工作。 The tests fail due to an exception: 测试由于异常而失败:
java.lang.NoClassDefFoundError: Could not initialize class javafx.scene.control.Label
Why does JUnit behave differently depending on how I run tests? 为什么JUnit的行为取决于我运行测试的方式? How can I fix this? 我怎样才能解决这个问题? Note that this is not in particular a GUI test per so (I do not want to simulate user actions or something like this, it is more about how certain controls are created and managed internally by my code). 请注意,这并不是特别的GUI测试(我不想模拟用户操作或类似的东西,它更多地是关于如何通过我的代码在内部创建和管理某些控件)。
Test suite: 测试套件:
@RunWith(Suite.class)
@SuiteClasses({
TestClass.class
})
public class TestSuite {
}
You are probably running your test suite under Java 7 (where you need to manually add the JavaFX runtime to the class path). 您可能正在Java 7下运行测试套件(您需要在其中手动将JavaFX运行时添加到类路径)。
I recommend doing all JavaFX development on a minimum Java release of Oracle Java 8 (where all of the JavaFX toolkit will automatically be on the default class path). 我建议在最低Java版本的Oracle Java 8上进行所有JavaFX开发(所有JavaFX工具包将自动位于默认类路径上)。
For more info see the answer to: Compile code using JavaFX 2.0 有关更多信息,请参见以下答案: 使用JavaFX 2.0编译代码
System.getProperty("java.version") returns 1.8.0_25, ie the version should not be a problem. System.getProperty(“ java.version”)返回1.8.0_25,即版本应该不是问题。
Could also be that you are using an OpenJDK build rather than an Oracle JDK build (as most OpenJDK implementations don't currently include JavaFX). 也可能是您使用的是OpenJDK构建而不是Oracle JDK构建(因为大多数OpenJDK实现当前不包括JavaFX)。
A likely issue is that your test suite is being run using a different class loader than the default class loader and the class loader the test suite is using does not include the JavaFX runtime classes (which in Java 8 are in the <JRE_HOME>/lib/ext/jfxrt.jar
). 一个可能的问题是,您的测试套件使用的是与默认类加载器不同的类加载器,并且该测试套件使用的类加载器不包括JavaFX运行时类(在Java 8中位于<JRE_HOME>/lib/ext/jfxrt.jar
)。
If you wish, you could edit your question to include an MCVE so that somebody could replicate your problem. 如果愿意,您可以编辑问题以包含MCVE,以便其他人可以复制您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.