Ed Taylor
Ed Taylor

Reputation: 108

Why Is assertThat Not Functioning Correctly When Checking For String Equality?

So a fairly simple one and I have resolved it by using assertEquals instead but just curious to know why this didn't work:

assertThat(list.get(0).getStringValue().equals("1234567811"));

The above line, after checking the variables during debugging, would count as a pass even when the String retrieved did not match.

When I use:

 assertEquals(list.get(0).getStringValue(), "1234567811");

It correctly picks up errors.

It was my understanding that assertThat should work for this too so I am curious as to why it doesn't?

Thanks

Upvotes: 3

Views: 1000

Answers (2)

mwarren
mwarren

Reputation: 759

It looks like AssertJ uses a fluent approach so you would need to do something like

assertThat(list.get(0).getStringValue()).isEqualTo("1234567811");

Upvotes: 2

el-aasi
el-aasi

Reputation: 312

you need to have this form for it to work properly

assertThat(actual, is(equalTo(expected)));

so it should be something like this

assertThat(list.get(0).getStringValue(), is("1234567811"));

or

assertThat(list.get(0).getStringValue(), equalTo("1234567811"));

can read more here

https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat-over-other-assert-methods-in-unit-tests/

Upvotes: 4

Related Questions