After you have finished this exercise, you understand how you can write assertions for maps with AssertJ. Let’s start by taking a look at the system under test.
The TagRepository
interface declares one method (findCountByTagName()
) that returns a map which describes how many times each tag has been used. If no tags is found from the database, this method returns an empty map.
For example, this method could return a map that has two key-value mappings:
Key | Value |
---|---|
code | 4 |
documentation | 1 |
The source code of the TagRepository
interface looks as follows:
import java.util.Map; interface TagRepository { Map<String, Integer> findCountByTagName(); }
The TagService
class has one method called findCountByTagName()
which simply invokes the findCountByTagName()
method of the TagRepository
interface and returns the map that’s returned by the invoked method.
The source code of the TagService
class looks as follows:
import java.util.Map; public class TagService { private final TagRepository repository; public TagService(TagRepository repository) { this.repository = repository; } public Map<String, Integer> findCountByTagName() { return repository.findCountByTagName(); } }
During this exercise you will write unit tests for the findCountByTagName()
method of the TagService
class. You can finish this exercise by following these steps:
1. Open the TagServiceTest
class that’s found from the com.cleantestautomation.junit5intro.tag
package.
2. Write the assertion which verifies that the system under test is working as expected when no tags is found from the database. You can find the relevant test method from this nested test class: FindCountByTagName -> WhenNoTagsIsFound
. Remember to use AssertJ.
3. Write the assertions which ensure that the system under test is working as expected when two tags are found from the database. You can find the relevant test methods from this nested test class: FindCountByTagName -> WhenTwoTagsAreFound
. Remember to use AssertJ.
4. Run the tests found from the TagServiceTest
class and make sure that they pass.