작성
·
801
0
@Test(expected = IllegalStateException.class)
public void 중복_회원_예외() throws Exception {
Member member1 = new Member();
member1.setName("kim");
Member member2 = new Member();
member2.setName("kim");
memberService.join(member1);
memberService.join(member2);
fail("예외가 발생해야 한다.");
}
위와 같이 작성하고 테스트 케이스를 실행시킨 경우에,
pass 표시가 떠야하지만
java.lang.Exception: Unexpected exception, expected<java.lang.IllegalStateException> but was<org.springframework.dao.InvalidDataAccessApiUsageException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:30)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: 이미 존재하는 ID입니다.; nested exception is java.lang.IllegalStateException: 이미 존재하는 ID입니다.
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
at icpbl2.module2.service.CustomerService$$EnhancerBySpringCGLIB$$582d39c5.join(<generated>)
at icpbl2.module2.service.CustomerServiceTest.중복_회원_예외(CustomerServiceTest.java:44)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 21 more
Caused by: java.lang.IllegalStateException: 이미 존재하는 ID입니다.
at icpbl2.module2.service.CustomerService.validateDuplicateCustomer(CustomerService.java:38)
at icpbl2.module2.service.CustomerService.join(CustomerService.java:25)
at icpbl2.module2.service.CustomerService$$FastClassBySpringCGLIB$$c9e25da.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
... 42 more
와 같은 오류메세지가 나오며 fail처리가 됩니다. 예외는 정상적으로 작동하는 것 같은데, (expected = IllegalStateException.class) 문구 및 직접 try catch 문구를 작성해도 저렇게 예외가 터집니다. 뭐가 문제일까요?
답변 3
1
0
같은 String 타입의 name을 비교하는 것과
같은 String 타입의 ID(PK가 아닌)를 비교하는것
둘다
private void validateDuplicateMember(Member member) {
List<Member> findMembers =
memberRepository.findByName(member.getName());
if (!findMembers.isEmpty()) {
throw new IllegalStateException("이미 존재하는 회원입니다.");
}
}
private void validateDuplicateCustomer(Customer customer) {
List<Customer> findCustomers =
customerRepository.findByUserId(customer.getUser_id());
if (!findCustomers.isEmpty()) {
throw new IllegalStateException("이미 존재하는 ID입니다.");
}
}
로 같은 로직입니다.
Member와 Customer 객체 역시 각 테이블 속성 타입이 일치하며 오로지 차이점은 변수명 밖에 없는 상황입니다.
테스트 케이스 작성시
@Test(expected = IllegalStateException.class)
public void 중복_회원_예외() throws Exception {
Member member1 = new Member();
member1.setName("kim");
Member member2 = new Member();
member2.setName("kim");
memberService.join(member1);
memberService.join(member2);
fail("예외가 발생해야 한다.");
}
@Test(expected = IllegalStateException.class)
public void 중복_회원_예외() throws Exception {
// Customer customer1 = new Customer("test", "testName1", LocalDate.of(1923, 02, 11), MemberStatus.CUSTOMER);
// Customer customer2 = new Customer("test", "testName2", LocalDate.of(1983, 02, 16), MemberStatus.CUSTOMER);
Customer customer1 = new Customer();
customer1.setUser_id("test");
Customer customer2 = new Customer();
customer2.setUser_id("test");
customerService.join(customer1);
customerService.join(customer2);
fail("예외가 발생해야 한다");
}
위와 같이 작성한 경우에 위는 pass 결과가 나오지만 밑의 경우에 fail 결과가 나옵니다. 무엇이 문제인가요?
build.gradle 및 application.yml 설정은 동일하게 진행하였습니다.(다만 사용하는 DB는 위의 경우엔 H2, 문제의 경우엔 Oracle DB를 사용하였습니다. H2가 아닌 Oracle인 경우엔 결과가 다를 수 있는 건가요?)