AssertTrue vs AssertEquals for ints [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-20 20:40:06

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/20990524/asserttrue-vs-assertequals-for-ints

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!