randomThought
randomThought

Reputation: 6393

Assert statement causing JUnit tests to stop

In the class I am testing, there are a few assert statements that check for various conditions.

One of the methods is

GetNames(string id){
    assert(! id.Equals("")); // Causes all junit tests to stop
    ...
}

and there is an assert statement to check if id is not blank.

In my unit tests, I have one test where I pass it a blank string, and a few others. The problem is that when the assert statement gets executed in the class, the JUnit tests stop running after that test case.

How can I change/setup the unit tests so that if there is an assertion failure in the class, the tests do not stop.

I have to enable the assertions in order for the code to run properly since there are cases where variables are incremented in the assertions. Unfortunately, I cannot change any of the code.

I am using the JUnit plugin in eclipse. I do not have any code in the setup and teardown sections.

Upvotes: 0

Views: 6717

Answers (1)

Richard Fearn
Richard Fearn

Reputation: 25511

If you have assert statements in the code being tested, and assertions are enabled, then those statements will be causing an AssertionError to be thrown. You can catch that in your test code and ignore it:

try {
    xxx.GetNames("");
} catch (AssertionError e) {
    // this is expected - ignore it
}

Upvotes: 4

Related Questions