Don't stop test after assertEquals

10,440

Solution 1

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);
}

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
10,440
jamesfzhang
Author by

jamesfzhang

Updated on June 04, 2022

Comments

  • jamesfzhang
    jamesfzhang almost 2 years

    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.

  • jamesfzhang
    jamesfzhang over 12 years
    I have a hunch that my test is failing because a variable's value is changing before it is supposed to. I'd like to see what happens after the test fails to further investigate this.
  • hvgotcodes
    hvgotcodes over 12 years
    then just comment out the line. You can also use the debugger to watch the value to find out when it changes.
  • jamesfzhang
    jamesfzhang over 12 years
    The other issue is that the test only fails sometimes. The variable that I am interested in changes value every 5 seconds give or take 300-500ms. As you can imagine, if the assert statement falls within the correct side of that gap, it'll pass, otherwise it'll fail. I cannot use the debugger because physically pressing a key to go to the next line is too slow.
  • Matthew Farwell
    Matthew Farwell over 12 years
    You're right that assertXXX throws an AssertionError, but this is caught by the JUnit framework, Eclipse gets notified because it has a RunListener