copenndthagen
copenndthagen

Reputation: 50732

Is the following assert syntax correct?

I have the following assert statement;

assertTrue(xyz.getTotalRecords() == 50);

I am getting an error

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:92)
    at org.junit.Assert.assertTrue(Assert.java:43)
    at org.junit.Assert.assertTrue(Assert.java:54)

Am I doing something wrong OR is it an issue with what is actually being asserted ?

Upvotes: 0

Views: 179

Answers (5)

Narendra Pathai
Narendra Pathai

Reputation: 41945

Am I doing something wrong OR is it an issue with what is actually being asserted ?

The syntax is fine. The problem is with assertion. Your expected value is not same as actual one.

Try providing a message for when assertion fails: JUnit

assertTrue("Expected size: 50, Actual size: " + xyz.getTotalRecords(), xyz.getTotalRecords() == 50);

or assertEquals("Expected size: 50, Actual size: " + xyz.getTotalRecords(), xyz.getTotalRecords(), 50);

Upvotes: 0

Alexey Andreev
Alexey Andreev

Reputation: 2085

You are doing all right. You just get failed assertion. BTW, good practice is to use Hamcrest that way:

import static org.hamcrest.CoreMatchers.*;
...
assertThat(xyz.getTotalRecords(), is(50));

Upvotes: 0

Arthur
Arthur

Reputation: 644

Good style is using assertEquals(xyz.getTotalRecords(), 50).

Upvotes: 1

Miquel
Miquel

Reputation: 15675

You get an assertion error because xyz.getTotalRecords() is not 50 but your assertion is otherwise correct, although you might want to use assertEquals instead

Upvotes: 2

Henry
Henry

Reputation: 43738

Since it compiled, the syntax is correct. You get this exception because the condition is not true.

Upvotes: 0

Related Questions