Conteúdo do Curso
Java JUnit Library. Types of Testing
Java JUnit Library. Types of Testing
Assertions
It's time to explore different types of assertions used in the JUnit library. You're already familiar with one of them, which is assertEquals()
, used to check the equality of values, determining the test's outcome. Every other assertion works in a similar way. If such an assertion evaluates to true
, the test passes; otherwise, it fails.
Let's go through different types of assertions one by one:
assertEquals
: Checks the equality of two values. You are already familiar with this type of assertion, but there's another interesting feature here. We can also output a message along with the error. It looks like this:
example.java
Here's what the test with a message output will look like in case the test fails. So, if the test evaluates to false, a message will be displayed, indicating how the test should have worked.
Testing.java
Here, the isEmpty()
method from the String
class is being tested. You can notice that a new assertion is used here, which we will now discuss:
assertTrue
: Checks that a condition istrue
. If the condition istrue
, the test passes successfully. This assertion works only withboolean
variables or conditions. An example of using such a condition can be seen above;
assertNull
: Checks that an object isnull
. If the object isnull
, the test passes successfully. This assertion can replace cases likeassertEquals(null, actual)
because it can be easily replaced with theassertNull
assertion.
fail
: Forcibly fails the test. It's straightforward here. I can't provide specific examples of when this might be used, but this assertion exists in the JUnit library, and I should tell you about it.
Example.java
Each of the assertions mentioned above has a counterpart. For instance, if we want to check the inequality of two values, we can use assertNotEquals
, whereas assertEquals
is used for checking equality.
Assertion | Purpose | Opposite |
---|---|---|
asserEquals() |
Checks the equality of two values. |
assertNotEquals()
|
assertTrue() |
Checks that a condition is `true`. |
assertFalse()
|
assertNull() |
Checks that an object is `null`. |
assertNotNull()
|
fail() |
Forcibly fails the test. | NaN |
This way, you can use their counterparts. These are the primary assertions used in JUnit, and you should know how to use each of them. In different situations, you can use different tests to make it clear to yourself and other programmers who will be reviewing your code what a particular test is checking.
Tudo estava claro?
Conteúdo do Curso
Java JUnit Library. Types of Testing
Java JUnit Library. Types of Testing
Assertions
It's time to explore different types of assertions used in the JUnit library. You're already familiar with one of them, which is assertEquals()
, used to check the equality of values, determining the test's outcome. Every other assertion works in a similar way. If such an assertion evaluates to true
, the test passes; otherwise, it fails.
Let's go through different types of assertions one by one:
assertEquals
: Checks the equality of two values. You are already familiar with this type of assertion, but there's another interesting feature here. We can also output a message along with the error. It looks like this:
example.java
Here's what the test with a message output will look like in case the test fails. So, if the test evaluates to false, a message will be displayed, indicating how the test should have worked.
Testing.java
Here, the isEmpty()
method from the String
class is being tested. You can notice that a new assertion is used here, which we will now discuss:
assertTrue
: Checks that a condition istrue
. If the condition istrue
, the test passes successfully. This assertion works only withboolean
variables or conditions. An example of using such a condition can be seen above;
assertNull
: Checks that an object isnull
. If the object isnull
, the test passes successfully. This assertion can replace cases likeassertEquals(null, actual)
because it can be easily replaced with theassertNull
assertion.
fail
: Forcibly fails the test. It's straightforward here. I can't provide specific examples of when this might be used, but this assertion exists in the JUnit library, and I should tell you about it.
Example.java
Each of the assertions mentioned above has a counterpart. For instance, if we want to check the inequality of two values, we can use assertNotEquals
, whereas assertEquals
is used for checking equality.
Assertion | Purpose | Opposite |
---|---|---|
asserEquals() |
Checks the equality of two values. |
assertNotEquals()
|
assertTrue() |
Checks that a condition is `true`. |
assertFalse()
|
assertNull() |
Checks that an object is `null`. |
assertNotNull()
|
fail() |
Forcibly fails the test. | NaN |
This way, you can use their counterparts. These are the primary assertions used in JUnit, and you should know how to use each of them. In different situations, you can use different tests to make it clear to yourself and other programmers who will be reviewing your code what a particular test is checking.
Tudo estava claro?