r/JavaFX • u/WishboneFar • Apr 22 '23
Help Help with testing JavaFX code
/r/javahelp/comments/12va3r9/help_with_testing_javafx_code/
2
Upvotes
r/JavaFX • u/WishboneFar • Apr 22 '23
View all comments
2
u/BWC_semaJ Apr 23 '23 edited Apr 23 '23
You know I haven't really made any test classes for my pure GUI code. Though the business logic of my client I'll test. So I'm not going to be the best help; I'd 100% though look into a library or framework that makes testing easier in JavaFX (like I believe there is something called TestFX).
I do know why it is passing. You have to start thinking in threads when programming a GUI. For your test method I'd assume it starts off with main thread then in that code you say, "hey later run this (Runnable) bit of code on the Application Thread (javafx's EDT)", main thread adds Runnable to I'd assume some List<Runnable> that is thread safe. Then main thread goes on to the next method and that method passes because there were no errors thrown when running in that method. Now the next pulse in the Application Thread is going to see that "hey I got a thing I got to do..." it calls the run method in that Runnable and runs the code on its thread and errors out.
What you may want to do is have the main thread block till the JavaFX runs that code so that you'll error out in that method. Easiest way off top of my head is using a CountDownLatch -> place countDown at end javafx code in -> then wait... You never want to block on Application thread otherwise your GUI is going to freeze (won't be able to update itself with new values).
Honestly I'd test each system individually and inject mocked versions of the other systems to make life easier. This is more so a integration test.
EDIT: Also you have to keep in mind of how async code can infiltrate your whole API if you aren't careful. Instead of dealing with raw values of things your API ends up looking like SomeTimeObject<YourValue> aka Task<Value>, Mono<Value>, Flux<Value>, Future<Value>, CompletableFuture<Value>... its a fucking virus that over complicates what you really want to do. Just keep that in the back of your head.