Ziwdigforbugs
Ziwdigforbugs

Reputation: 1214

Issue with assertion in webdriver and TestNG

I am facing a wired situation while doing an assertion. I am asserting the value inside each list (Lists are supposed to contain same value even though that are not List of same objects)

Here is my assertion code :

for (int i=0 ; i< rst.size(); i++)

         {
             Assert.assertSame(l.get(i).getName(),rst.get(i).getText());

Here is the response I am getting :

FAILED: srch("tsk", "http://frstmwarwebsrv2.orsyptst.com:9000/duobject?   searchString=TSK&filtercheck=nameSWF&p.index=0&p.size=8")
java.lang.AssertionError: expected [TSK(ACE700J)(000)(ACE700JU00)(000)] but found    [TSK(ACE700J)(000)(ACE700JU00)(000)]
         }

I have changed the above as advised by I am still getting the same error :

Assert.assertSame ((l.get(i).getName().trim()),  rst.get(i).getText().trim());

Upvotes: 1

Views: 842

Answers (1)

James Dunn
James Dunn

Reputation: 8264

The methods getName() and getText() both return Strings, so (assuming your problem is only leading and trailing white space) all you need to do is add .trim(), like so:

Assert.assertEquals(l.get(i).getName().trim(),rst.get(i).getText().trim());

It seems, also, that what you want to do is test that different objects in memory are meaningfully equal. This means you want to use assertEquals(), not assertSame().

  • assertEquals() asserts that two objects, which may or may not be different objects in memory, are meaningfully equal (or, as you put it, have the same value).
  • assertSame() asserts that two objects are in fact the same object in memory -- so if you run assertSame() on two objects with "the same value" but which are not the same object in memory, your test will fail.

Upvotes: 1

Related Questions