开发者

Eclipse JUNIT: Why is it possible to sometimes do a compare on a failed assertequals(String,String) but not always

开发者 https://www.devze.com 2023-03-24 07:21 出处:网络
I thought at first the compare option only appears for a test if the assertequals is between 2 strings, which makes sense. I believe i have also fired a failNotEquals(String1,String2) and the compare

I thought at first the compare option only appears for a test if the assertequals is between 2 strings, which makes sense. I believe i have also fired a failNotEquals(String1,String2) and the compare option comes up as well. I have done this when the object does not implement equals or perhaps i wish to compare for equality using my own rules and failing if that fails. However i have noticed a few times here开发者_开发技巧 and there sometimes failNotequals(String,String) does not enable the "compare" option. Am i missing something ???

CLARIFICATION of COMPARE option The >compare< option is a menu option that is available when one right clicks on a failed junit item from the gui where it creates a tree with items for each "test". Often theres also a >print stack trace< option.


You get the "compare" option, when the end result of the test is a junit.framework.ComparisonFailure object. In that case you can see a visual comparison of the two objects (Strings for example) and check how they differ from each other.

Other types of results, such as a java.lang.IllegalAccessError, aren't parsed to that effect, as Eclipse doesn't know (and cannot deduce) that you were comparing two objects that lead to such a result, so it doesn't offer you the same visual boons.


I am not entirely sure what you mean by

failNotEquals does not enable the "compare" option

Here are a couple of thoughts.

  1. failNotEquals takes three arguments, not two:

    failNotEquals(message, expected, actual);

  2. Even if strings would print out the same, they are NOT the same object. You need to tell JUnit how to compare them.

Consider this small, working test case.

import junit.framework.TestCase;

public class DemoTest extends TestCase {
  public void testSomething() {
    String a = "Hello";
    assertTrue("Strings should be the same", "Hello".equals(a));
    failNotEquals("Strings should be the same", "Hello", a);
  }
}

The assertTrue works, but the failNotEquals does not.

The assertTrue is probably what you want. The "Hello".equals(a) will compare the strings properly. You can also use a equalsIgnoreCase here.

I'm guessing that the failNotEquals is probably close to what you are using. It fails with a java.lang.IllegalAccessError.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号