1

it isn't a new issue for sure, but I couldn't find a nice solution for my problem, so I will try to find one on my own.

I have given an Application class:

import javafx.application.Application;
import javafx.stage.Stage;

public class TestApplication extends Application {

    @Override
    public void init() throws Exception {
        // ...
        super.init();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // ...
    }

    @Override
    public void stop() throws Exception {
        // ...
        super.stop();
    }
}

Now, I want to test methods from its controller and so on.. Not the GUI, but other things.

So, for this, I thought, I create an instance of this Application, start it and then I make something like instance.getXY(...). Is it the right purpose?

A little example for a JUnit class:

import org.junit.Test;

public class JUnitTests {

    @Test
    public void testFoo() {
        // this starts the application, but I don't get a reference from it
        javafx.application.Application.launch(TestApplication.class);
    }
}

I've found this Launch JavaFX application from another class to get an refrence of the Application class. But it don't seems to be a nice solution for me.

How would you fix my testing problem?

Thanks for your help!

Community
  • 1
  • 1
mrbela
  • 4,477
  • 9
  • 44
  • 79
  • Why do you need an instance of the Application for testing? – Michael Lloyd Lee mlk Feb 26 '16 at 10:45
  • Because the Application is starting a network connection, is sending different things to another point, gets responses and so on.. So, I want to simulate something like "the app is starting", "the user is sending something" (by invoking the sending method out of JUnit), then I would wait a few seconds and proof if a response came in. If I make this out of a JUnit test class, I need a reference of the Application class, don't I? – mrbela Feb 26 '16 at 10:48
  • I can't see your code base, but I'd except you to be able to fake such calls in other ways. – Michael Lloyd Lee mlk Feb 26 '16 at 10:52
  • Mh, but for this calls I need for example the network connection session built by the application. – mrbela Feb 26 '16 at 11:28
  • Why are you doing things like that in the `Application` class though? I would factor those out into a service class of some kind. Then you can just unit test the service class etc. The `Application` subclass should just have minimal code to invoke startup and shutdown functionality on other classes. – James_D Feb 26 '16 at 13:25

0 Answers0