StringBuffer problem in unit test

344 views
Skip to first unread message

Si Westcott

unread,
Jan 3, 2010, 6:40:18 PM1/3/10
to Sheffield Java User Group
Hey all,

I've been fighting what should be a simple unit test for too long and
would appreciate some guidance please :) I've whittled it down to the
following code,

@Test
public void testStringBuffer() {
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
assertEquals(sb1, sb2);
}

Given this, JUnit reports tells me:

java.lang.AssertionError: expected: java.lang.StringBuilder<> but was:
java.lang.StringBuilder<>

Si

Stuart Grimshaw

unread,
Jan 3, 2010, 6:43:58 PM1/3/10
to java-sh...@googlegroups.com
What happens if you initialise the 2 stringbuffers with the same value?

@Test
public void testStringBuffer() {
       StringBuffer sb1 = new StringBuffer("Fert");
       StringBuffer sb2 = new StringBuffer("Fert");
       assertEquals(sb1, sb2);
}



-S

Follow me on Twitter: http://twitter.com/stubbs
Blog: http://stubblog.wordpress.com
My art: http://stuartgrimshaw.imagekind.com
Stock Images: http://en.fotolia.com/partner/16775

brabster

unread,
Jan 3, 2010, 6:58:37 PM1/3/10
to Sheffield Java User Group
Hey Si,

StringBuffer doesn't override the equals(Object o) method from the
Object class.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

The Object implementation of equals returns true when the object and
the argument refer to the same object instance. That means that if sb1
and sb2 are references to StringBuffer instances, if you call
sb1.equals(sb2) it will never return true unless sb1 and sb2 refer to
the same StringBuffer instance.

That's why your test is failing - they are not referring to the same
object. The confusing error message? Dunno, I would expect just a
JUnit thing.

Cheers

Si Westcott

unread,
Jan 4, 2010, 4:36:32 AM1/4/10
to Sheffield Java User Group
@Stu - I get the same error.

@Paul - Makes sense now, I was comparing two resource handles/
references rather than the contents of the buffer.

So to make it work I need:

assertEquals(sb1.toString(), sb2toString());

Cheers,
Si

brabster

unread,
Jan 4, 2010, 1:57:21 PM1/4/10
to Sheffield Java User Group
Indeed.

Probably no need to but it confused me at first, so I'll just stress
that it *coulda* returned true... if StringBuffer had overridden the
equals method to do so.

Worth knowing how equals, hashCode and == work, can help you avoid
confusing results later particularly with hash based collections like
HashMap and HashSet. Maybe start on developerworks
http://www.ibm.com/developerworks/library/j-jtp05273.html

Cheers

James Jefferies

unread,
Jan 4, 2010, 4:14:30 PM1/4/10
to Sheffield Java User Group
Good old hashcode and equals! Have you got hold of Effective Java yet?
Lots of good stuff in there about those two old chesnuts...

Si Westcott

unread,
Jan 4, 2010, 6:33:52 PM1/4/10
to Sheffield Java User Group
On 4 Jan, 21:14, James Jefferies <jamesjeffer...@gmail.com> wrote:
> Good old hashcode and equals! Have you got hold of Effective Java yet?
> Lots of good stuff in there about those two old chesnuts...

Aye, it's beside me now. It's not quite what I was expecting tbh, but
it's good so far :)

Reply all
Reply to author
Forward
0 new messages