Writing assertEquals in Kotlin
The assertEquals
statement is widely used for testing code. It basically takes in two arguments—an expected value and an actual value—with an optional third argument message. If the expected value matches the actual value, the assertEquals
passes—otherwise, it fails.
Using assertEquals
with primitive types is straightforward, but if you want to use it with a custom object, you'll have to do a little more work. For example, the following assertEquals
will not pass:
assertEquals(MyObj("abc"),MyObj("abc"))
In this recipe, we will learn how to write assertEquals
statements.
Getting ready
We'll be using Android Studio 3.0 for our coding purposes. You can download the source code from https://gitlab.com/aanandshekharroy/Anko-examples and switch to the 5-instrumentation-tests
branch.
How to do it…
Let's go through the following steps to understand assertEquals
:
- In the following code, if you run the given
assertEquals
, it will not pass:
assertEquals(MyObj("abc"),MyObj("abc...