Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Assertions | Unit Tests
Java JUnit Library. Types of Testing
course content

Зміст курсу

Java JUnit Library. Types of Testing

Java JUnit Library. Types of Testing

1. Testing in Development
2. Unit Tests
3. Exceptions

bookAssertions

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:
java

example

copy
12
assertEquals(expected, actual); assertEquals(message, expected, actual);

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.

java

Testing

copy
1234567891011
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class Testing { @Test public void testIsEmpty_True() { String emptyString = ""; assertTrue(emptyString.isEmpty(), "The string should be empty"); } }

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 is true. If the condition is true, the test passes successfully. This assertion works only with boolean variables or conditions. An example of using such a condition can be seen above;
  • assertNull: Checks that an object is null. If the object is null, the test passes successfully. This assertion can replace cases like assertEquals(null, actual) because it can be easily replaced with the assertNull 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.

java

Example

copy
12
fail(); fail(message);

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.

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.

1. Which assertion method in JUnit is used to verify that a condition is `true`?
2. What is the purpose of the `assertFalse` assertion in JUnit?
3. How is `assertNotEquals` used in a JUnit test case?
Which assertion method in JUnit is used to verify that a condition is `true`?

Which assertion method in JUnit is used to verify that a condition is true?

Виберіть правильну відповідь

What is the purpose of the `assertFalse` assertion in JUnit?

What is the purpose of the assertFalse assertion in JUnit?

Виберіть правильну відповідь

How is `assertNotEquals` used in a JUnit test case?

How is assertNotEquals used in a JUnit test case?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
some-alt