Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit - assertSame

Can someone tell me why assertSame() do fail when I use values > 127?

        import static org.junit.Assert.*;

        ...

        @Test
        public void StationTest1() {
          ..

          assertSame(4, 4);         // OK
          assertSame(10, 10);       // OK
          assertSame(100, 100);     // OK
          assertSame(127, 127);     // OK
          assertSame(128, 128);           // raises an junit.framework.AssertionFailedError!
          assertSame(((int) 128),((int) 128)); // also junit.framework.AssertionFailedError!
        }

I'm using JUnit 4.8.1.

like image 970
Michael W. Avatar asked May 21 '10 13:05

Michael W.


People also ask

What is assertSame in JUnit?

assertSame. public static void assertSame(java.lang.Object expected, java.lang.Object actual) Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown. Parameters: expected - the expected object actual - the object to compare to expected.

What does assertSame mean?

assertSame: Asserts that two objects refer to the same object. In other words. assertEquals: uses the equals() method, or if no equals() method was overridden, compares the reference between the 2 objects.

What does assertSame () method used for assertion?

What does assertSame() method use for assertion? Explanation: == is used to compare the objects not the content. assertSame() method compares to check if actual and expected are the same objects.

How do you assertTrue in JUnit?

assertTrue() If you wish to pass the parameter value as True for a specific condition invoked in a method, then you can make use of the. JUnit assertTrue(). You can make use of JUnit assertTrue() in two practical scenarios. By passing condition as a boolean parameter used to assert in JUnit with the assertTrue method.


2 Answers

The reason is the autoboxing of Java. You use the method:

public static void assertSame(Object expected, Object actual) 

It only works with Objects. When you pass ints to this method, Java automatically calls

Integer.valueOf( int i ) 

with these values. So the cast to int has no effect.

For values less than 128 Java has a cache, so assertSame() compares the Integer object with itself. For values greater than 127 Java creates new instances, so assertSame() compares an Integer object with another. Because they are not the same instance, the assertSame() method returns false.

You should use the method:

public static void assertEquals(Object expected, Object actual) 

instead. This method uses the equals() method from Object.

like image 110
Daniel Engmann Avatar answered Sep 22 '22 21:09

Daniel Engmann


assertSame takes two Object arguments, and so the compiler has to autobox your int literals into Integer.

This is equivalent to

assertSame(Integer.valueOf(128), Integer.valueOf(128)); 

Now for values between -128 and 127, the JVM will cache the results of Integer.valueOf, so you get the same Integer object back each time. For values outside of that range, you get new objects back.

So for assertSame(127, 127), JUnit is comparing the same objects, hence it works. For assertSame(128, 128), you get different objects, so it fails.

Just another reason to be careful with autoboxing.

like image 31
skaffman Avatar answered Sep 21 '22 21:09

skaffman