EntityとRepositoryの作成とテスト
15051 ワード
1.Entityの作成
事前に作成されたERDに基づいてドメインを作成@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Todo extends BaseEntity {
@Id @GeneratedValue
private Long seq;
@Builder
public Todo(String name, String content, TodoStatus status, Integer predictedPomo, Integer realPomo, WorkProficiency workProficiency, WorkType workType, WorkImportance workImportance) {
this.name = name;
this.content = content;
this.status = status;
this.predictedPomo = predictedPomo;
this.realPomo = realPomo;
this.workProficiency = workProficiency;
this.workType = workType;
this.workImportance = workImportance;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="group_seq")
private Group group;
private String name, content;
private TodoStatus status;
private Integer predictedPomo, realPomo;
private WorkProficiency workProficiency;
private WorkType workType;
private WorkImportance workImportance;
}
[ Annotation ]
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Todo extends BaseEntity {
@Id @GeneratedValue
private Long seq;
@Builder
public Todo(String name, String content, TodoStatus status, Integer predictedPomo, Integer realPomo, WorkProficiency workProficiency, WorkType workType, WorkImportance workImportance) {
this.name = name;
this.content = content;
this.status = status;
this.predictedPomo = predictedPomo;
this.realPomo = realPomo;
this.workProficiency = workProficiency;
this.workType = workType;
this.workImportance = workImportance;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="group_seq")
private Group group;
private String name, content;
private TodoStatus status;
private Integer predictedPomo, realPomo;
private WorkProficiency workProficiency;
private WorkType workType;
private WorkImportance workImportance;
}
( More: https://yoonbing9.tistory.com/28 )
パラメータが
任意の順序のオブジェクトを作成可能
[関連マッピング]
Todoとグループの関係は一方向N:1
クエリー
マッピングする外部キーの名前を指定して
2.レポートの作成 public interface TodoRepository extends JpaRepository<Todo, Long> {}
JPA Repositoryを継承するインタフェースの作成
public interface TodoRepository extends JpaRepository<Todo, Long> {}
3.Repositoryユニットのテスト @DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class TodoRepositoryTest {
@Autowired
private TodoRepository todoRepository;
@Test
void testCreateTodo() {
// given
Todo todo = Todo.builder()
.name("할일")
.content("내용")
.predictedPomo(1)
.realPomo(1)
.status(TodoStatus.TODO)
.workImportance(WorkImportance.LOWER)
.workProficiency(WorkProficiency.ADVANCED)
.build();
// when
Todo savedTodo = todoRepository.save(todo);
// then
assertThat(savedTodo.getName()).isEqualTo("할일");
assertThat(savedTodo.getContent()).isEqualTo("내용");
assertThat(savedTodo.getPredictedPomo()).isEqualTo(1);
assertThat(savedTodo.getRealPomo()).isEqualTo(1);
assertThat(savedTodo.getStatus()).isEqualTo(TodoStatus.TODO);
assertThat(savedTodo.getWorkImportance()).isEqualTo(WorkImportance.LOWER);
assertThat(savedTodo.getWorkProficiency()).isEqualTo(WorkProficiency.ADVANCED);
}
}
Reference
この問題について(EntityとRepositoryの作成とテスト), 我々は、より多くの情報をここで見つけました
https://velog.io/@dot2__/Entity-생성-및-Repository-테스트
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class TodoRepositoryTest {
@Autowired
private TodoRepository todoRepository;
@Test
void testCreateTodo() {
// given
Todo todo = Todo.builder()
.name("할일")
.content("내용")
.predictedPomo(1)
.realPomo(1)
.status(TodoStatus.TODO)
.workImportance(WorkImportance.LOWER)
.workProficiency(WorkProficiency.ADVANCED)
.build();
// when
Todo savedTodo = todoRepository.save(todo);
// then
assertThat(savedTodo.getName()).isEqualTo("할일");
assertThat(savedTodo.getContent()).isEqualTo("내용");
assertThat(savedTodo.getPredictedPomo()).isEqualTo(1);
assertThat(savedTodo.getRealPomo()).isEqualTo(1);
assertThat(savedTodo.getStatus()).isEqualTo(TodoStatus.TODO);
assertThat(savedTodo.getWorkImportance()).isEqualTo(WorkImportance.LOWER);
assertThat(savedTodo.getWorkProficiency()).isEqualTo(WorkProficiency.ADVANCED);
}
}
Reference
この問題について(EntityとRepositoryの作成とテスト), 我々は、より多くの情報をここで見つけました https://velog.io/@dot2__/Entity-생성-및-Repository-테스트テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol