Test Overview
This test checks if two object references point to the exact same object using assertSame, and verifies that two references do not point to the same object using assertNotSame.
This test checks if two object references point to the exact same object using assertSame, and verifies that two references do not point to the same object using assertNotSame.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class ReferenceTest { @Test void testAssertSameAndNotSame() { String a = "hello"; String b = a; String c = new String("hello"); // assertSame passes because a and b refer to the same object assertSame(a, b, "a and b should be the same object"); // assertNotSame passes because a and c are different objects assertNotSame(a, c, "a and c should not be the same object"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | - | PASS |
| 2 | Assign String 'hello' to variable a | Variable a references String object "hello" | - | PASS |
| 3 | Assign variable b to reference the same object as a | Variable b references the same String object as a | - | PASS |
| 4 | Create new String object with value 'hello' and assign to c | Variable c references a new String object with value "hello" | - | PASS |
| 5 | assertSame(a, b) checks if a and b reference the same object | a and b reference the same String object | assertSame passes because references are identical | PASS |
| 6 | assertNotSame(a, c) checks if a and c reference different objects | a and c reference different String objects | assertNotSame passes because references are different | PASS |
| 7 | Test ends successfully | All assertions passed | - | PASS |