Skip to main content
gamedev.net
🔒 Locked

Unit Test Java Swing

Started by Glass_Knife Nov 30, 2005 at 12:38 PM 2 replies 1k views
Original Post
Glass_Knife
Glass_Knife
I have looked at various tools for testing swing code, but none of them are easy to use, and require tons of code to setup. Event for a small UI it is hard to keep the test code current. Does anyone use a good (free) Swing testing tool? I have many books on TDD, and most of them say "Don't test GUI code." Although most of the things I do do not require a UI to test them, some things are hard to test. Has anyone been successful with a UI that didn't need to be tested?
capn_midnight
capn_midnight
yes, it's quite easy to build GUI's that do not require testing. Write all your "business logic" in a seperate class from the control frame. Test the business logic class, make the wired up connections to the front end for user input, and trust that clicking a button actually fires an event.

I find this especially true for Java, as I think code gets really cluttered when you put all your logic in with your GUI.

But sometimes, your tests take a lot of work. That doesn't mean they aren't worth doing.
Glass_Knife
Glass_Knife
Quote:
Original post by capn_midnight
Write all your "business logic" in a seperate class from the control frame. Test the business logic class, make the wired up connections to the front end for user input, and trust that clicking a button actually fires an event.


I agree with this, and I try and do this as much as I can, but there are still things that the GUI needs to do that need to be tested. For instance, in one app, a file is sent to a server, and the progress is displayed on the GUI with a progress bar. There is lots of logic to handle the progress bar in the GUI code. Also, I have many places where depending on the state of the DataObject, controls are enabled/disabled. How can this stuff be tested without testing the GUI.

capn_midnight
capn_midnight
Quote:
Original post by Glass_Knife
Quote:
Original post by capn_midnight
Write all your "business logic" in a seperate class from the control frame. Test the business logic class, make the wired up connections to the front end for user input, and trust that clicking a button actually fires an event.


I agree with this, and I try and do this as much as I can, but there are still things that the GUI needs to do that need to be tested. For instance, in one app, a file is sent to a server, and the progress is displayed on the GUI with a progress bar. There is lots of logic to handle the progress bar in the GUI code. Also, I have many places where depending on the state of the DataObject, controls are enabled/disabled. How can this stuff be tested without testing the GUI.


like I said, the actual operation of the GUI is not your code, you don't need to test it. Just trust it works. Make sure that you test for returning the states properly. Also, you can try mock objects for the GUI elements. They are objects that utilize your business logic in the same way in which a GUI would utilize it, but you have complete programmatic control over the objects. Interfacing will go a long way to help you with this.

If a certain set of conditions means "set control to disabled", then test those conditions. Don't worry about testing to see if setting the enabled property to false actually makes the control unusable: that is assumed.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.