oe a
oe a

Reputation: 2280

Comparing two string failing assertion

This is really weird, im trying to assert two strings are equal and it's failing even though it looks to be the same.

    Assert.assertSame("Extra Spicy", type, "type is not extra spicy");

I get this error:

java.lang.AssertionError: type is not extra spicy expected [Extra Spicy] but found [Extra Spicy]
Expected :Extra Spicy
Actual :Extra Spicy

Everything matches, why is it failing?

Upvotes: 5

Views: 5229

Answers (2)

Pratik Rawlekar
Pratik Rawlekar

Reputation: 327

assertSame asserts that two objects refer to the same object. If they are not the same an AssertionFailedError is thrown.

You can also check equality as follows

    Assert.assertEquals("Extra Spicy", "type is not extra spicy");

Upvotes: 0

souldzin
souldzin

Reputation: 1476

Assert.assertSame uses the == operator, which checks that two objects are the same object (have the same reference).

I think you want to use Assert.assertEquals which uses the equals() method, checking if the value of two objects are equal or not.

JUnit has some very helpful examples on their github:

https://github.com/junit-team/junit/wiki/Assertions

Upvotes: 7

Related Questions