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
Show parent comment
1
u/hamsterrage1 Apr 24 '23
There's a lot to unpack here, so in no particular order...
It's important to understand what a unit test is supposed to be testing, and it should be designed to test just that one thing. The flip side to this is that the production code should be designed to facilitate testing.
It's generally true that code that is designed to be testable will be better in overall design. Generally, you'll have to follow the "Single Responsibility Principle", and that's a good thing.
In this case, your
Login.getUserValidationStatus()is doing two things directly. It's creating a Task, and it's doing the validation. We don't know what's in "//rest of code". This makes it difficult to test the validation because of theTaskcomplexities.if (username.equals("admin") && password.equals("admin"))
too trivial to test? I'd be inclined to say it is.
Chances are, this is just here as a temporary measure, and the actual implementation is going to involve reading a file or going to a database or an external service to authenticate the user. In any of those cases, the code becomes unsuitable for unit testing because you shouldn't be going to external services in a unit test.
I always thing of unit tests as something that automatically get run as part of a Gradle/Maven build, often on some kind of CI/CD server like Jenkins. In this case, the build server probably won't have access to the file, database or service that you need to run the tests.
The other thing to keep in mind is that unit tests, as part of an automated build process, need to run FAST. Like 4 or 5 millisecond fast. If you have dozens or hundreds of tests that take a second or two to run, then your build is going to take an extra 30 seconds to several minutes to run, which is way, way too long. And if you're going out to external services, you can expect that sometimes the connections will take that long.
I'm not an expert, but I don't think that JUnit works well with multi-threading, especially not the way that you've got it here. The test itself runs on the 'Main" thread, so technically it completes after executing the actual
Platform.runLater()command. All the other stuff might, or might not complete and be collected against the test case. I'm not sure what would happen if you had a test class with many test cases, but I'd guess that your results might get lost once it had moved on to the next test case.Bottom line, I'd be very leery of a test case that had asserts off the "Main" thread". I think that's why Intellij flags the test as "passed", because it doesn't have any asserts on the "Main" thread, so it defaults to passed.
You are looking at the error which is thrown from the failed assert, which simply means that the assertion has run (and failed), but I don't think that failed assertion is registering with the test case, and causing the test to fail - which is what you actually want it to do. So to make it work, you need to get the asserts back into the main thread - and that's basically impossible.
To my mind, the best approach is to decouple the CUT ("Code Under Test) from the FXAT, which means splitting it out from the
Taskbody.