0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

@DapaJpaTest で H2 を使って永続化層をテストする

0
Posted at

前提

Java SpringBoot において、永続化層のテストを行いたい。
永続化層の役割は、ドメインのエンティティの状態を保存し、同じ状態で取り出すことである。それをテストする。

前提となるエンティティの実装はこちらを参照。

サンプルコード

@DataJpaTest
class UserRepositoryTest {

  @Autowired UserRepository repository;

  @Test
  void should_save_and_retrieve() {
    UUID id = UUID.randomUUID();
    User user = new EmployeeUser(id, "Taro", "25001");
    repository.save(user);

    User retrievedUser = repository.findById(id).orElse(null);

    assertNotNull(retrievedUser);
    assertEquals(id, retrievedUser.getId());
    assertEquals("Taro", retrievedUser.getName());
    assertInstanceOf(EmployeeUser.class, retrievedUser);
    assertEquals("25001", ((EmployeeUser) retrievedUser).getEmployeeId());
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?