웹개발/스프링부트

RepositoryTest Code

sunhoKim 2021. 7. 17. 19:18

//등록 작업 테스트

@Test

public void testInsertDummies() {

 

IntStream.rangeClosed(1, 100).forEach(i -> {

Memo memo = Memo.builder().memoText("Sample..." + i).build();

memoRepository.save(memo);

});

}

 

등록 작업 테스트 결과

 

//조회 작업 테스트

@Test

public void testSelect() {

 

// 데이터베이스에 존재하는 mno

Long mno = 100L;

 

Optional<Memo> result = memoRepository.findById(mno);

 

System.out.println("-------------------------------------------");

 

if (result.isPresent()) {

Memo memo = result.get();

 

System.out.println(memo);

}

 

}

 

fineById

 

 

//getOne()의 경우,Transactiona은 트랜잭션 처리를 위해 사용되는 어노테이션

//getOne()의 경우, 리턴 값은 해당 객체이지만, 실제 객체가 필요한 순간까지 SQL을 실행하지 않는다.

@Transactional

@Test

public void testSelect2(){

Long mno = 100L;

 

Memo memo = memoRepository.getOne(mno);

 

System.out.println("-----------------------------");

 

System.out.println(memo);

}

getOne()

 

 

// 수정 작업 테스트

// JPA는 엔티티 객체들을 메모리상에 보관하려고 하기 때문에 특정한 엔티티 객체가 존재하는지 확인하는

// select가 먼저 실행되고 해당@Id를 가진 엔티티 객체가 있다면, update, 그렇지 않다면 insert를 실행

@Test

public void testUpdate() {

Memo memo = Memo.builder().mno(100L).memoText("Update Text").build();

 

System.out.println(memoRepository.save(memo));

}

update()

 

// 삭제 작업 테스트

@Test

public void testDelete() {

Long mno = 5L;

 

memoRepository.deleteById(mno);

 

}

delete()

 

728x90

'웹개발 > 스프링부트' 카테고리의 다른 글

[SpringBoot] Sort test Code  (0) 2021.07.24
[SpringBoot] Paging test Code  (0) 2021.07.24
application.properties  (0) 2021.07.11
엔티티 클래스와 JpaRepository  (0) 2021.07.11
Spring Data JPA  (0) 2021.07.11