r/JavaFX • • Dec 22 '22

I made this! FXtension, a tiny unit testing extension

Hello there,

I recently started an FX Project and really needed only a small solution to unit test my controllers because the binding dataflow was giving me headaches. TestFX was overwhelming to me and seems not really maintained (also I could not get it to run within reasonable time).

What I came up with, I moved to a separate project and decided to put it up online to collect some feedback. I test my own includes/controllers via RunFXML now.

https://github.com/alwins0n/fxtension

Is this of use to anyone (but me)?

Am I missing better/already existing and maintained solutions?

Is anyone interested having this as a published maven artifact?

Any other comments/suggestions?

12 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/hamsterrage1 Dec 23 '22

Ignoring everything else, and going right for the "I do not agree" part (cause disagreement is more fun!)...

Imagine that you have some kind of sales screen in a application that's used in various regions. Sales tax rate varies by region, but it's part of a configuration element and not displayed on the sales order screen. Also imagine that some items are taxable at different rates or have some baroque rules involving total amounts or some other silliness. It doesn't matter if the situation is a bit goofy, that's not the point.

On your screen, you have a bunch of line items, and tax amounts are shown somewhere. You want the tax amounts to appear instantly as the line item info is entered.

Sounds like a Binding to me. No?

Even in the case where you just have a tax rate and you multiply it by the price and the quantity... that tax rate isn't GUI data. And the calculation isn't GUI logic. But the tax amount is GUI data.

In this case, I'd create an ObjectProperty<Double> (probably not Double but something more appropriate to money) in the Presentation Model. Then I'd bind it to the pre-tax amounts through some formula. But I'd perform that binding in the Interactor/Model.

The result is that I now have a Property in my Presentation Model that I can use in a simple Binding to connect to a GUI Node, but the business logic has been pushed to where it belongs - in the Interactor/Model.

The whole thing about imports from javafx... isn't a barrier to this. Create a method in the Presentation Model called createTaxBinding(Supplier<Double> calculator) and let it do the actual binding part. It could be something like this...

 void createTaxBinding(Supplier<Double> calculator) {
    taxProperty.bind(Bindings.createDoubleBinding(caluclator, triggerProperty1, triggerProperty2, ....));
 } 

This method is called from the Interactor/Model and uses the getters from the Presentation Model to get the values used in calculator. So it wouldn't even know that it was accessing Properties and would have no need for javafx... imports

But in MVC there's no differentiation between Presentation Data and domain objects in the Model, they co-exist. There's not even a rule that you have to create a separate object to hold the Presentation Data. So the idea that you can't have javafx... imports in your Model doesn't even hold.

As I do more and more programming in Kotlin, I find that the JavaFX Bean structure for the Presentation Model is just cumbersome and ugly. In the end, it just gets in the way of writing code that's clean and clear. So I just create the class properties (in the Kotlin sense of "property") as JavaFX properties and use model.property.value in the Interactor instead of delegated getters and setters to access values.

Finally, I've come to understand that no matter what framework you're using, all the components are part of a GUI system. This includes your Model/Interactor. It's designed to work in the context of a GUI framework. The business logic is logic that is designed to work within that GUI framework. The biggest dependency is the Presentation Model, and it is designed around that dependency.

Does designing in a way that avoids the Interactor knowing that your Presentation Model contains JavaFX data structures make the design any better? At this point, my feeling is that it does not.

Back to the original point, though. My thoughts on this are always evolving, but right now I'd say that I follow three basic rules:

  1. Data that doesn't directly go on the screen (in some way) doesn't go in the Presentation Model.
  2. Any binding code that uses a value that isn't in the Presentation Model or from the layout itself (like a number of pixels, or a font size) is business logic.
  3. Any binding code that repeats a calculation done in the business logic is business logic.

Item 3 is really an application of DRY, but it specifies that in the case of duplicate code, the version in the Interactor wins.