Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert statement Assert.assertSame is failing

I have the following code in a junit test case. The first Assert passes and the second one fails.

final int code = webResponse.getResponseCode();
Assert.assertTrue(200 == code);  //passes
Assert.assertSame(200, code);    //fails

Why does the second one fail? webResponse is type WebResponse and all implementations of getResponseCode return an int.

I am running the code within a junit test and the second assert fails in both Intellij and Eclipse IDE. Also, in Intellij, it provides a link to "Click to see difference" but when I click that, it says "Contents are identical".

like image 781
jlars62 Avatar asked Jan 10 '23 10:01

jlars62


1 Answers

assertSame(Object, Object) checks if both arguments refer to the same object.

It performs boxing conversion to convert 200 to a valid reference type object. To do this, it does

Integer.valueOf(200);

and

Integer.valueOf(code);

which return new object references which do not reference the same object.

like image 142
Sotirios Delimanolis Avatar answered Jan 16 '23 10:01

Sotirios Delimanolis