简体   繁体   English

如果作为测试套件的一部分运行,JUnit测试将失败

[英]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.

相关问题 在Maven中,如何作为测试套件的一部分而不是单独运行JUnit测试用例? - In Maven how to run a JUnit test case as part of a test suite and not alone? gradle测试任务如何运行特定的junit测试套件? - how can gradle test task run specific junit tests' suite? 为什么 JUnit 5 Suite 注释 @SelectClasses 和 @IncludeClassNamePatterns 找不到不以“Tests”或“Test”结尾的测试? - Why do JUnit 5 Suite annotations @SelectClasses and @IncludeClassNamePatterns fail to find tests that don't end in “Tests” or “Test”? 如果同一测试多次运行,如何区分junit测试运行于哪个测试套件? - How distinguish from which test suite was junit test run in case the same tests are run more times? Selenium Eclipse Test Suite测试在套件中失败,但在单独运行时成功 - Selenium Eclipse Test Suite tests fail in the suite, but are successful when run individually Mockito 测试单独通过但作为套件的一部分失败 - Mockito Tests Pass individually but fail as part of a suite JUnit 4:在测试运行之前在测试套件中设置东西(比如测试的@BeforeClass方法,仅​​用于测试套件) - JUnit 4: Set up things in a test suite before tests are run (like a test's @BeforeClass method, just for a test suite) JUnit测试未运行-返回“空测试套件” - JUnit Tests not running - Returning “Empty test suite” 如何在JUnit 4测试套件中获得测试集合 - How to get a collection of tests in a JUnit 4 test suite 从命令行运行JUnit Test suite - Run JUnit Test suite from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM