On Fri, 25 Nov 2011 15:28:43 +0100, Emilis <[email protected]> wrote:

Hello, Posse people,
I would like to get some opinions on how to write the unit tests for
the following scenario.

Let's assume that we are testing some method which should generate to
events: eventA and eventB. Unit test could look like this:

testEventA() {
  // 1. mock eventA listener
  // 2. setup the test case
  // 3. invoke method
  // 4. verify that eventA listener was called correctly
  // 5. verify that eventA contains correct values
}

testEventB() {
  // 1. mock eventB listener
  // 2. setup the test case
  // 3. invoke method
  // 4. verify that eventB listener was called correctly
  // 5. verify that eventB contains correct values
}

Assuming that steps 2 and 3 are the same for both tests, do you keep
both tests separate (as above) or do you write testEventAandEventB()
instead? and why?
Bonus question. Let's assume that both tests are testing the same
event now and the only difference between them is steps 2 and 3. What
would be your approach? again, why?

Separate tests in any case if they can fail independently. This doesn't mean that you can't factor out some common preparation / assertion code, of course. If for some reason they cannot fail independently, or there's no strong advantage in knowing that only one failed, I'd prefer to keep the same two separate tests, but with a lesser strength.


--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
[email protected]
http://tidalwave.it - http://fabriziogiudici.it

--
You received this message because you are subscribed to the Google Groups "The Java 
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to