From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Prevent tasks with duplicate IDs

Prevent tasks with duplicate IDs

- [Instructor] One consequence of using a hash table for our task management is that each task id must be unique. Otherwise, tasks might get overwritten when we add them to the hash table. Let's create a test for this case. We'll set up two tasks with the same id. We'll call them task1 and task2. Let's run this test and it passes. Technically, our contains operation uses the task id to check if a given task exists in the hash map, so it does work as expected, but what we really want to verify is that the number of tasks has increased in the task manager. Let's create a method that returns the number of tasks. We'll use the size of the hash map as a way to check how many tasks are in the task manager. Let's add an assertion to our test. We'll assert that there are two tasks in the task manager. Let's run it again. We've entered the red phase. Our test fails. There's only one task in our manager instead of two. To fix this, instead of passing the id to the task constructor, we could…

Contents