Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+13 votes

I am writing test cases for our group's application and I wonder if we should write test cases for the @FXML methods. For example, in my scenerio training mode, I set @FXML void transmitTaskFromBot() as a function that check if the radio's filter is at the same frequency of the bot(s). If so, the screen will start printing out some task and playing sound. My question is that do people write test case for @FXML function?

To be more specific, the logic to check for validity of radio-bots frequency matching is in that @FXML function, and I want to test the logic of when should the radio receive the task from bot and play sound. Should I create a helper method in the data model and test that sub-method, or is there any way other than assertEquals() that can helps me test the logic within a @FXML?

asked in CSC 305 Fall 2024 by (1.9k points)

1 Answer

+6 votes
 
Best answer

Generally, I wouldn't recommend writing unit test for the @FXML methods directly.

Instead, it would be better to encapsulate any interesting logic/functionality that is currently in an @FXML event handler into a different method (or even better, a different class, like a HamRadio class), and do your unit testing on that.

The @FXML event handling method would then likely only contain a few of lines of code, which would just call methods that get the real job done elsewhere.

Unit testing tends to work best on your data model classes, rather than your controller classes.

However, it is a good goal to extract as much logic code out of the UI as you can, which gives you a richer data model, and gives you more things you can test automatically in that data model.

answered by (5.7k points)
selected by
...