This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Advice needed on unit-testing code that uses Swing.

2 replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
First, let me make clear; I'm not doing UI or high-level testing, here, just standard unit tests to ensure that classes/methods/whatever behave as expected under known input conditions. No user interaction, none of that hard stuff :-).
However, the underlying data structures are tightly coupled to Swing constructs, and it's not really sensible to try to decouple them. (It's possible to an extent, but it would add a significant amount of code, and at the same time limit the amount of testing I could do.)
So what I need is simply advice on how to:
    1) Start up and initialize Swing under a test framework (preferably Scalatest)    2) Do anything special that might be necessary while the tests are running, to accommodate Swing.    3) Shut everything down cleanly.
Is this possible? It would be a big help.
ThanksKen
Matthew Farwell
Joined: 2011-08-11,
User offline. Last seen 34 weeks 5 days ago.
Re: Advice needed on unit-testing code that uses Swing.
You could look at FEST http://code.google.com/p/fest/, which is a framework for testing Swing applications. It is mainly for testing the UI though, I don't know how easy it would be to use for what you want.
Matthew.

Le 4 octobre 2011 23:03, Ken McDonald <ykkenmcd [at] gmail [dot] com> a écrit :
First, let me make clear; I'm not doing UI or high-level testing, here, just standard unit tests to ensure that classes/methods/whatever behave as expected under known input conditions. No user interaction, none of that hard stuff :-).
However, the underlying data structures are tightly coupled to Swing constructs, and it's not really sensible to try to decouple them. (It's possible to an extent, but it would add a significant amount of code, and at the same time limit the amount of testing I could do.)
So what I need is simply advice on how to:
    1) Start up and initialize Swing under a test framework (preferably Scalatest)    2) Do anything special that might be necessary while the tests are running, to accommodate Swing.     3) Shut everything down cleanly.
Is this possible? It would be a big help.
ThanksKen

Bill Venners
Joined: 2008-12-18,
User offline. Last seen 31 weeks 5 days ago.
Re: Advice needed on unit-testing code that uses Swing.

Hi Ken,

Sorry for taking a day to respond to this. Did you get a solution going?

I'm not sure what exactly you need special to deal with Swing if
you're not doing GUI testing, other than perhaps something to make it
easy to check things on Swing's event handler thread. Seems like
that's just a simple wrapper around the methods you usually use to do
things on the event handler thread. I imagine what might be useful is
something like an onEventThread method that takes a by-name, invokes
it on the event thread via SwingUtiltiies.invokeAndWait, and possibly
unwraps the exception out of the InvocationTargetException and
rethrows that. Does that sound useful, or do you need something else?

Bill

On Tue, Oct 4, 2011 at 2:03 PM, Ken McDonald wrote:
> First, let me make clear; I'm not doing UI or high-level testing, here, just
> standard unit tests to ensure that classes/methods/whatever behave as
> expected under known input conditions. No user interaction, none of that
> hard stuff :-).
> However, the underlying data structures are tightly coupled to Swing
> constructs, and it's not really sensible to try to decouple them. (It's
> possible to an extent, but it would add a significant amount of code, and at
> the same time limit the amount of testing I could do.)
> So what I need is simply advice on how to:
>     1) Start up and initialize Swing under a test framework (preferably
> Scalatest)
>     2) Do anything special that might be necessary while the tests are
> running, to accommodate Swing.
>     3) Shut everything down cleanly.
> Is this possible? It would be a big help.
> Thanks
> Ken

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland