0

I have the following code in one of my tests. The second assertion fails.

String decode1 = URLDecoder.decode("a%20b", "UTF-8");

assertTrue("a b".equals(decode1)); // pass
assertEquals("a‌ b", decode1); // FAIL

I can't figure out why. Is this a bug with JUnit? You would think that JUnit would just call .equals() if the first arg is not null...

Ben
  • 7,692
  • 15
  • 49
  • 64

2 Answers2

2

When I pasted your code into a new test, it fails as you stated. However, after deleting the final "a b" string and retyping it, it passed. You must have some embedded characters in there that I don't see when viewing the source of this page.

The failed assertion shows this somewhat by saying the expected value is

"a[ ]b"
David Harkness
  • 35,992
  • 10
  • 112
  • 134
0

It does call equals(). Looking at the source code from kickjava, we have the following. (You didn't say if you are using JUnit 3.8 or 4.0, but I'm pretty sure they didn't change the implementation of something this basic.

public static void assertEquals(String   msg, Object   obj1, Object   obj2) {

        if (obj1 == null && obj2 == null) {
          return;
        }

        if (obj1 != null && obj1.equals(obj2)) {
          return;
        }

        fail(msg + " expected=" + obj1 + " actual=" + obj2);
}

Are you 100% sure a stray encoded character didn't sneak into your test. Maybe try this to rule it out?

String expected = "a b";
assertTrue(expected.equals(decode1)); 
assertEquals(expected, decode1); 
Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59