Friday, 13 March 2020

TestNG: assertSame: Asserts that two references refer to the same object

‘assertSame’ methos asserts that two references refer to the same object. This method uses == operator to check for equality.

'assertSame' method is available in below overloaded forms.
public static void assertSame(Object actual, Object expected, String message)
public static void assertSame(Object actual, Object expected)

AssertSameTest.java
package com.sample.app.assertions;

import static org.testng.Assert.assertSame;

import org.testng.annotations.Test;

public class AssertSameTest {

 @Test
 public void test1() {
  Object obj1 = new Object();
  Object obj2 = new Object();

  assertSame(obj1, obj2, "obj1 and obj2 are not refering to same object");

 }

 @Test
 public void test2() {
  Object obj1 = new Object();
  Object obj2 = obj1;

  assertSame(obj1, obj2);

 }

}

Run AssertSameTest.java, you will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
PASSED: test2
FAILED: test1
java.lang.AssertionError: obj1 and obj2 are not refering to same object expected [java.lang.Object@4923ab24] but found [java.lang.Object@44c8afef]
 at org.testng.Assert.fail(Assert.java:97)
 at org.testng.Assert.failNotSame(Assert.java:965)
 at org.testng.Assert.assertSame(Assert.java:914)
 at com.sample.app.assertions.AssertSameTest.test1(AssertSameTest.java:14)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================



Previous                                                    Next                                                    Home

No comments:

Post a Comment