In this chapter we are going to create the dataaccess
layer of our application backend.
We create the entities and repositories as well as the corresponding database schema and initial data.
After you have setup the project, we are going to create our first app component.
In your application backend create the package org.example.app.task.dataaccess
(in src/main/java
).
Inside this package create the following data-model:
Extend your entity classes from ApplicationPersistenceEntity
to inherit id
and version
.
For each entity create a corresponding repository interface in the same package extending JpaRepository
.
Fill in the entity as first generic argument and use Long
as second argument for the type of the primary key as in the following example:
public interface TaskListRepository extends JpaRepository<TaskListEntity, Long> {
}
We have already created SQLs for you with DDL and testdata so you do not have to worry about it.
You can find this in src/main/resources/db/migration
Now you can create JUnit tests (based on assertJ using Assertions
class to allow assertThat
usage). An example is the following code.
@QuarkusTest
public class TaskItemRepositoryTest extends Assertions {
@Inject
private TaskItemRepository taskItemRepository;
@Test
public void testFindById() {
// given
Long itemId = 11L;
// when
TaskItemEntity item = this.taskItemRepository.findById(itemId).get();
// then
assertThat(item.getTitle()).isEqualTo("Milk");
}
}
Here are some ideas, what you can test:
-
Give me all TaskLists without the TaskItems
-
Add an TaskItem to an existing nonempty TaskList
-
Find all TaskLists, with TaskItems having a due date that expires in the next week
-
Find all TaskLists, that have open TaskItems
-
Delete all TaskItems that are completed