What is the actual difference between assertEquals

2019-02-12 10:19发布

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?

标签: testng
1条回答
forever°为你锁心
2楼-- · 2019-02-12 10:42

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.)

查看更多
登录 后发表回答