views:

49

answers:

4

Hello,

I've been given a bunch of messy code and a short time limit (no surprises there) to write some tests for it. I have written tests! They are good tests.

Unfortunately, instantiating some of the project's components causes Swing GUI elements to be constructed and set visible too. I don't want this to happen for obvious reasons, so I was wondering if there was a way to suppress the displaying of any Swing-based stuff before I instantiate these objects. Essentially, some kind of master visibility setting, that says "I don't care if anyone calls setVisible on a Swing component, don't show anything."

I don't think there is, and I don't think there's a solution other than modifying the project code. Just thought I'd ask.

A: 

Don't know if this will help, but if the frame itself isn't visible, then setting a Swing component (JTextField, JButton....) visible has no effect.

camickr
+1  A: 

I'm not quite into your exact problem, but you can extend your component classes (which in turn extend JComponent) and override the setVisible() method with an empty implementation. The new classes, of course, will reside in the test package.

Otherwise, it's strongly recommended to refactor the components, instead of doing 'hacks' just in order to test them.

Bozho
I'll try this too, I think - the problem is that I don't think I'll be able to stop it being setVisible in the first place.
mtc06
A: 

Maybe setting the mainFrame (the container to all subcomponents) visibility to false, should not break the code, while leaving you with less UI.

 mainFrame = new JFrame();
 ....
 // Commeted out for tests
 // mainFrame.setVisible(true);
 mainFrame.setVisible(false);
The MYYN
+1  A: 

Well I won't answer what I want (which is change the code to separate the GUI from the work so it is testable)... but you might consider headless mode. Off the top of my head you will have to wrap the parts of the GUI that fail as a result by catching the HeadlessException.

There is a good chance that such a thing breaks the code in the testing environment however since things will have to probably be set to null.

I short I think you can make that work, but the result will be messier code... but if you don't have time to fix it I guess that might work.

TofuBeer
Excitingly, this very nearly worked. I'll experiment with this and get back to you.
mtc06