📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Check out JUnit 5 tutorials and examples at JUnit 5 TutorialIn JUnit 5, all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.
When to use assertSame() method
Assert.assertSame(Object expected, Object actual) method
- expected - the expected object
- actual - the object to compare to expected
Assert.assertSame() method example
import static org.junit.Assert.assertSame;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class AssertSameExample {
private String processMap(final String key){
final Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
map.put("key5", "value5");
map.put("key6", "value6");
map.put("key7", "value7");
map.put("key8", "value8");
return map.get(key);
}
@Test
public void checkSameReferenceTest(){
final AssertSameExample example = new AssertSameExample();
assertSame(example.processMap("key1"), example.processMap("key1"));
}
}
Output:
Related JUnit Examples
- JUnit Assert.assertArrayEquals() Method Example
- JUnit Assert.assertEquals() Method Example
- JUnit Assert.assertTrue() Method Example
- JUnit Assert.assertFalse() Method Example
- JUnit Assert.assertNull() Method Example
- JUnit Assert.assertNotNull() Method Example
- JUnit Assert.assertSame() Method Example
- JUnit Assert.assertNotSame() Method Example
- JUnit Assert.fail() Method Example
Comments
Post a Comment
Leave Comment