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

Course Content

Java JUnit Library. Types of Testing

Java JUnit Library. Types of Testing

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

book@BeforeEach

Let's continue creating unit tests for the test class and the method that sorts palindromes in reverse order by length. We have one method that checks the functionality of this method. We need to test a few more cases of how this method works:

  1. Test the method's functionality when we pass an empty list into it;
  2. Test the method's functionality when we pass a list without palindromes;
  3. Test the method's functionality when we pass a list of palindromes with different lengths.

Note

When we create methods, we need to give them appropriate names that fully reflect how the method works. For example, in the case of an empty list, we will name the unit test like this: testFilterAndSortPalindromesWithEmptyList.

Let's start with the second test case when we pass an empty list to the method:

As you can see, we are passing empty lists as both " input " and " expected ." In other words, we expect the method to return an empty list. This is exactly what the test is checking; we pass an empty list and expect to receive an empty list in return.

Next, let's create a test that will pass a list that doesn't contain any palindromes at all. In this case, we also expect to receive an empty list:

Note

Pay attention to the naming of this test. The name indicates what the test is doing and which case is being tested.

Here, we are also testing this method with the expectation of receiving an empty list. In the case where there are no palindromes, the method should not return a list since all elements will be filtered out during sorting.

@BeforeEach

You may have noticed that in each test, we follow the same set of actions. In the JUnit framework, there is an annotation that allows us to simplify this process slightly. More specifically, we automate the process of creating input, expected, and actual variables using the @BeforeEach annotation. The actions specified in the method annotated with this annotation will be executed before each test.

For example, we can specify the creation of three lists, like this:

We've annotated the SetUp() method with @BeforeEach, which means that this method will be executed each time before every test method. Having such a method, we can remove the list creation from each of the tests, thereby making our work a bit easier.

But that will be your task.

Task

Your task is to remove the list creation from the tests and write a unit test that will check if the sorting in the method works correctly. In other words, you need to create a test that will verify how the method behaves with palindromes of different lengths.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt