영상에서는 회원가입 테스트 시 rollback 전에 insert를 하지 않았지만, 스프링 부트 2.7.5에서는 insert 진행 후 rollback합니다.
MemberServiceTest
@SpringBootTest
@Transactional
class MemberServiceTest {
@Autowired
MemberService memberService;
@Autowired
MemberRepository memberRepository;
@Test
void 회원가입() {
//given
Member member = new Member();
member.setName("oh");
//when
Long savedId = memberService.join(member);
//then
assertEquals(member, memberRepository.findOne(savedId));
}
}
Console
2022-11-23 09:05:16.978 INFO 14200 --- [ Test worker] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@6f80fafe testClass = MemberServiceTest, testInstance = jpabook.jpashop.service.MemberServiceTest@18139a43, testMethod = 회원가입@MemberServiceTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3af17be2 testClass = MemberServiceTest, locations = '{}', classes = '{class jpabook.jpashop.JpashopApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@797cf65c, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@a22cb6a, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6187d1f5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@46944ca9, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2a898881, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@6283d8b8], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@27107a97]; rollback [true]
2022-11-23 09:05:18.516 DEBUG 14200 --- [ Test worker] org.hibernate.SQL :
select
member0_.member_id as member_i1_4_,
member0_.city as city2_4_,
member0_.street as street3_4_,
member0_.zipcode as zipcode4_4_,
member0_.name as name5_4_
from
member member0_
where
member0_.name=?
2022-11-23 09:05:18.539 INFO 14200 --- [ Test worker] p6spy : #1669161918539 | took 8ms | statement | connection 3| url jdbc:h2:tcp://localhost/~/jpashop
select member0_.member_id as member_i1_4_, member0_.city as city2_4_, member0_.street as street3_4_, member0_.zipcode as zipcode4_4_, member0_.name as name5_4_ from member member0_ where member0_.name=?
select member0_.member_id as member_i1_4_, member0_.city as city2_4_, member0_.street as street3_4_, member0_.zipcode as zipcode4_4_, member0_.name as name5_4_ from member member0_ where member0_.name='oh';
2022-11-23 09:05:18.567 DEBUG 14200 --- [ Test worker] org.hibernate.SQL :
insert
into
member
(member_id, city, street, zipcode, name)
values
(default, ?, ?, ?, ?)
2022-11-23 09:05:18.569 INFO 14200 --- [ Test worker] p6spy : #1669161918569 | took 0ms | statement | connection 3| url jdbc:h2:tcp://localhost/~/jpashop
insert into member (member_id, city, street, zipcode, name) values (default, ?, ?, ?, ?)
insert into member (member_id, city, street, zipcode, name) values (default, NULL, NULL, NULL, 'oh');
2022-11-23 09:05:18.586 INFO 14200 --- [ Test worker] p6spy : #1669161918586 | took 0ms | rollback | connection 3| url jdbc:h2:tcp://localhost/~/jpashop
;
2022-11-23 09:05:18.589 INFO 14200 --- [ Test worker] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@6f80fafe testClass = MemberServiceTest, testInstance = jpabook.jpashop.service.MemberServiceTest@18139a43, testMethod = 회원가입@MemberServiceTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3af17be2 testClass = MemberServiceTest, locations = '{}', classes = '{class jpabook.jpashop.JpashopApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@797cf65c, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@a22cb6a, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6187d1f5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@46944ca9, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2a898881, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@6283d8b8], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]
안녕하세요. 데스나이트님
전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.
https://bit.ly/3fX6ygx
주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요
추가로 다음 내용도 코멘트 부탁드립니다.
1. 실행 방법을 알려주세요.
2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.
감사합니다.
답글
데스나이트
2022.11.28안녕하세요. 영한님.
답변을 주셔서 댓글 쓰는 중에 저의 코드가 강의 내 코드와 다른 점을 확인하였습니다.
아래 코드에서 저는 @GeneratedValue 전략을 IDENTITY로 설정 하였습니다.
키 값을 DBMS에게 위임하여 가져오도록 하는 것이 문제였던 것으로 확인 됩니다.
( 여러 테이블의 id값을 1번부터 보고 싶은 마음에 설정하였습니다. ^^; )
IDENTITY 전략을 제거 후 실행해보니 insert를 실행하지 않았습니다.
JPA는 편해지는 만큼 정확히 알고 세심하게 다뤄야 하는 것 같습니다.
답글 감사드립니다.