Testing multiple JavaFX components using JemmyFX

I am working on a rather large project that includes a set of custom JavaFX components. For each custom component that is intended to be reused, I started writing a set of automated tests using JemmyFX and JUnit. During development, I run them from Eclipse Juno.

Performing the entire test suite immediately proved difficult. The problem, apparently, is that since I want to test several components, I would ideally use different applications for each (so that tests performed on one component do not affect other tests).

I created a base class that does the following:

  @BeforeClass
 public static void beforeClass () {

     Thread t = new Thread ("JavaFX Init Thread") {

         @Override
         public void run () {
             Application.launch (UITester.class, new String [0]);
         }
     };

     t.setDaemon (true);
     t.start ();
 }

Using this base class, I created a separate class with @Test tags for each custom control. When I run this test suite, the first test script works fine, but the rest do not work:

  Exception in thread "JavaFX Init Thread" java.lang.IllegalStateException: Application launch must not be called more than once

I tried the following ways to solve this problem:

Application shutdown

I added the following to the base class:

  @AfterClass
 public static void afterClass () {
     Platform.exit ();
 }

The same problem persists. Perhaps because the VM does not reboot between tests?

Put protection against restarting the application

I set a static variable to check if the application is possibly already running. This makes the problem go away when I run tests from Eclipse. When I run them from the command line, the problem still exists. Not good when we try to run these tests on the integration server.

Catch IllegalStateException

It is odd. I can catch an exception, and most of my problems go away, except for the fact that every 4 or 5 sessions of the entire Ubuntu test suite crashes into the shell and I need to log in again.

So, what is the best way to write tests for a large set of custom controls? Is my approach wrong?

+6
source share
1 answer

Looking at the source of MarvinFX , I was able to implement our test environment in such a way as to solve my problems. Which, apparently, to the greatest extent helped to eliminate this problem, was to restore the scene and scene for each test, as shown in this (pseudo) code:

 @Before public void before() { Node node = generateComponentToTest(); Parent parent = StackPaneBuilder.create().children(node).build(); Scene scene = SceneBuilder.create().root(parent).build(); if (this.currentStage != null) { this.currentStage.close(); } Stage stage = new Stage(); stage.setScene(scene); stage.centerOnScreen(); stage.show(); this.currentStage = stage; } 
+3
source

Source: https://habr.com/ru/post/954218/


All Articles