@Test
public void save() {
Member member = new Member();
member.setName("spring");
memberRepository.save(member);
Member result = memberRepository.findById(member.getId()).get();
System.out.println("result=" + (result==member));
//sout를 통한 메소드 검증
//출력: result=true
Assertions.assertEquals(member, result);
//junit의 Assertions 기능을 통한 메소드 검증
//result와 member가 일치하는지 확인
//출력: Run창에 Test result 옆에 초록색 표시가 뜸
// 틀릴 경우 빨간색 표시
Assertions.assertThat(member).isEqualTo(result);
//asserj의 Assertions 기능을 통한 메소드 검증
}
}
@Test
public void findByName() {
Member member1 = new Member();
member1.setName("spring1");
memberRepository.save(member1);
Member member2 = new Member();
member2.setName("spring2");
memberRepository.save(member2);
Member result = memberRepository.findByName("spring1").get();
assertThat(member1).isEqualTo(result);
}
memberRepository.findByName("spring1");
Optional<Member> spring1 = memberRepository.findByName("spring1");
// ctrl+alt+V 사용시 반환타입이 Optional 반환됨
memberRepository.findByName("spring1").get();
Member spring1 = memberRepository.findByName("spring1").get();
//get() 사용시 Optional 내부의 Member가 반환됨
@AfterEach
public void afterEach() {
memberRepository.clearStore();
}
public void clearStore() {
store.clear();
}
참조
스프링 입문 - 김영한