Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertTrue vs AssertEquals for ints [closed]

Tags:

junit

Should we use assertEquals or assertTrue for comparing primitive types specifically ints? Is there a preference, if so why ? I'd like to know the pros and cons of each approach.

like image 207
Phoenix Avatar asked Jan 08 '14 08:01

Phoenix


2 Answers

assertEquals() gives a useful default error message on failure, like "expected X but got Y", but assertTrue() can't. Use the more specific applicable method here, which is assertEquals().

like image 180
Sean Owen Avatar answered Dec 25 '22 17:12

Sean Owen


assertEquals() is to test the equality of your expected value with the returning value. Whereas assertTrue() is to check for a condition. Having said that, you can also say

If you have a condition like.

String x = "abc";
String y = "abc";

assertEquals(x, y);

You can also change it to

assertTrue(x.equals(y));

It is just another way of asserting what you expect.

like image 35
Hrishikesh Avatar answered Dec 25 '22 18:12

Hrishikesh