Reputation: 4483
I am trying to debug a failing test that has an assertEquals
JUnit test. I'd like to see the behavior of the application for a certain amount of time after the assertEquals
statement fails. Right now, eClipse stops the test as soon as the statement fails. Is there a way to make the test sleep for a bit after a failure? Thanks.
Upvotes: 2
Views: 12717
Reputation: 2656
assertEquals throws an AssertionError, so you need to find a way to react to that exception.
You're IDE might have the ability to add a break point when an exception or error is thown. If it does, then add one for AssertionError.
If it doesn't then you could add a try catch around the assertEquals, and add a break point in the catch block.
Upvotes: 1
Reputation: 16262
In general, your unit tests should have a single assertion in them; a single thing they're testing. Its not unreasonable to have two tests that have exactly the same code in them except for the assertion, ie "When this is run, these two things should both be true" being two tests.
It's worth bearing in mind that, if what you're really looking for with the additional code (past the assertion) is to help you determine WHY the test failed, consider the ever popular printf-debugging. You can add some output to the test before the assertion to let you know the state of thing. You can always pull it back out once the test is working.
Upvotes: -2
Reputation: 51123
When you say "the behavior of the application for a certain amount of time", you're talking about behavior in a different (non-test) thread, right? I'd extract the assertion conditions into a variable, put a breakpoint on the actual assert line, and run the test in the debugger:
@Test
public void testSomeMethod() {
assertEquals(true, testInstance.someMethod());
}
becomes
@Test
public void testSomeMethod() {
boolean result = testInstance.someMethod();
/* breakpoint goes here */ assertEquals(true, result);
}
Upvotes: 2
Reputation: 15579
ideally your test should have just one assert per tests and your test should only cover only one specific functionality at a time.
if you have more than one test then it might very well be the case that your other assets failed cos of the first failure and thus there is no value in having the other tests.
But if you still wanna go ahead and do it well then you could probably build up your own assert library where in you create your own apis and store the results somewhere and then print out the error. not too close to java so not sure abt how it can be coded.
Upvotes: 0
Reputation: 340763
Not really. Behind the scenes JUnit framework throws an exception when assertion fails. Eclipse catches this exception and marks given test as failed. So you are asking: is it possible to suppress thrown exception and continue execution?
JUnit won't help you here. You can replace JUnit assertions with your own wrappers that simply do not throw an exception (or throw when test ends). Of course you can also comment out test failures. But there is no built in solution.
Upvotes: 1
Reputation: 120198
the simplest thing to do is to comment out the line.
An individual test will fail as soon as an assertion fails, the reason being how can you trust any results after the first failure?
You said
I'd like to see the behavior of the application for a certain amount of time after the assertEquals statement fails
I don't understand that statement. Are you running an app with junit assertions, or are you running a suite of unit tests? Also, if the assertion fails, why do you want to continue? Sounds like your test design needs some more work -- an assertion failure means the situation is a failure, period. If you want to see what happens after the failure, then you should add more detail to the assertion and only fail when you don't want to proceed.
Upvotes: 1