Re: JUNIT questions



On Feb 28, 11:18 am, Mark Jeffcoat <jeffc...@xxxxxxxxxxxxxxx> wrote:
"Taria" <mche...@xxxxxxxxxxx> writes:
Did it? Well, sorta. The red bar flashes at me and I am clueless how
to go about debugging an error that JUNIT has found. All the
tutorials I have gone through usually addresses the green bar but not
what to do when the method is not functioning properly. My experience
with past debuggers is that some step you thru the program's execution
and you are able to set up breaks, examine values of variables, etc.
So, how do you use JUNIT the same way? Am I looking at JUNIT the
wrong way?

JUnit's not a debugger; it runs tests suites, and reports
on whether they pass or fail.

If they fail, JUnit can report which test failed, along with
the difference between the expected value and the actual value
or the exception that was thrown. (I run JUnit from Ant, so
I don't know how you'd get that information in your IDE.)

If you'd like to use a debugger to fix the failure, you're
welcome to do it, but you need to go find a debugger. After
you've made your changes, use JUnit to re-run your tests.

--
Mark Jeffcoat
Austin, TX

The latest Eclipse and IntelliJ IDEs can also run your JUnit tests
themselves. Because of this integration, you can simply double click
on the Junit failure/exception message and they open the necessary
file with the focus on the appropriate method.

Its very handy

For the benefit of the OP, on the debugging issue, I've long stopped
using a debugger by creating one single junit test and writing just
enough to make it pass, rinse & repeat. If the small amount of code I
write to make the test pass, doesn't work, and I can't easily see
whats wrong... I delete it and start again. Sounds crazy I know, but
it tends to be faster than debugging.

'Fake it until you make it' is key to this. It allows use to write
'Just Enough to Make it Pass'.

E.g.

Start with a test..

void testAddingTwoPositives() {
assertEquals("Wrong answer", 2, Calc.add(1, 1) );
}


'fake it until you make it'...

Class Calc {
public static int add(int first, int second) {
return 2;
}
}


All tests pass at this point and it took no time at all...

Now lets write a test to force us to create it...


void testAddingTwoNegatives() {
assertEquals("Wrong answer", -2, Calc.add(-1, -1) );
}

Test fails...

so change the Calc implementation....

Class Calc {
public static int add(int first, int second) {
return first + second;
}
}


now all tests pass!

This is a small example, but it should show you how you can write unit
tests, without having the implementation there to make them work. And
it shows how you can make them work quickly, but not fully, so that
later tests force your design to become better.

Andrew

.



Relevant Pages

  • Re: JUNIT questions
    ... I thought it wasn't...thank you for confirming my suspicion. ... use JUnit to re-run your tests. ... I'm happy to use a debugger because a debugger has always been my ... Andrew for that hint how to use JUNIT without using a debugger. ...
    (comp.lang.java.programmer)
  • Re: JUNIT questions
    ... how do you use JUNIT the same way? ... it runs tests suites, and reports ... on whether they pass or fail. ... If you'd like to use a debugger to fix the failure, ...
    (comp.lang.java.programmer)