Al Lelopath
Al Lelopath

Reputation: 6778

Unit test for string value

This test fails:

String validString = new String("12345.78");  
myClass.setStringValue(validString);  
assertEquals( "testSetStringValue: " + validString, validString, myclass.getStringValue() ); 

because, I assume, even though validString and the return value from myclass.getStringValue() contain the same contents 12345.78, they are different objects.

How do I test for them containing the same thing?

Upvotes: 0

Views: 1335

Answers (2)

kan
kan

Reputation: 28981

Just use assertSame. But I don't think it makes sense, because it doesn't agree with String contract. Any of your code should not assume strings are ==, it must rely on that they are equal.

Upvotes: 1

user180100
user180100

Reputation:

assertEquals() will use equals() for the comparison, so your test is OK. If you want to test that some object instance is the same as some other, you can use assertSame()

see JUnit doc for more infos.

Upvotes: 4

Related Questions