I'm confusing about both these methods, because both can do the same thing, like below snippet of my code.
Using assertEquals()
String a = "Hello";
String b = "Hello";
assertEquals(a, b);
Using assertTrue()
assertTrue(a.equals(b));
Can anyone tell me the actual difference between both these two methods?
assertEquals
is better because it gives the unit test framework more information about what you're actually interested in. That allows it to provide better error information when the test fails.
Suppose you had
String a = "Hello";
String b = "Hi";
Then the test failures might look something like:
// From assertEquals(a, b)
Error: Expected "Hi"; was "Hello"
// From assertTrue:
Error: Expected true; was false
Which of those do you think gives you more information, bearing in mind that the values would probably be the result of reasonably complex computations?
(These are made up error messages as I don't have testng installed, but they're the kind of thing unit test frameworks give.)
See more on this question at Stackoverflow