What may one need the assertThat() method for? I did it without calling that method, and Java did the conversion just fine
From what I can see, the
assertThat() method is just called so that on running the code, it is clear that
string is in fact a string, and therefore that the
charArray has successfully been converted to a string…
But what does it do?
From a bit of reading, it seems that
assertThat() is often used when writing unit tests. Essentially, it takes the first argument (in your case that’s the variable
string), and compares it to the second argument (in your case that’s
is("baeldung")). Where it differs from a simple comparison using
== is that the second argument doesn’t have to be a value, for example, it could be a type, such as
isA(String.class), which would then check if the first argument was a string. From what I’ve read, I don’t think it necessarily adds new functionality to Java per se, it just makes certain code more readable. Here’s a decent article on it, if you want more reading.
Thank you! Why do you think I can’t import it?
Am I supposed to do this?
Well, I did it and then imported
org.junit.Assert;, but Java still can’t resolve the
is() method unless I import it too. The problem is I don’t know which one I should import, there are a few of them!
Anyway, I imported the first one. But IntelliJ IDEA tells me now something about
assertThat() being deprecated, whatever that means! What does it entail and should I be concerned?
I also found out that if the two are not equal, it returns an exception, not a
false boolean value
Deprecated just means that the feature is no longer maintained, and doesn’t appear in later versions of the language. While it is something to look out for, it isn’t something to be too concerned about, especially if you’re building something for production.
As for the exception, I can’t really explain that, but I imagine it’s because you’re testing, which I suppose means that it’s better to have an exception than a log that you might miss?