I want to write a test for the following method:
public Account getAccount(Long id) {
log.debug("Getting account with id {}", id);
Optional<Account> optionalAccount = accountRepository.findById(id);
if (!optionalAccount.isPresent()) {
log.warn("Account with id {} not found", id);
throw new EntityNotFoundException();
} else
return optionalAccount.get();
}
For this I wrote the following test method. When I use Mockito everything works fine with mocking but when I want to use EasyMock I get a EntityNotFoundException.
@Test
public void whenCreate_ThenRoleShouldBeFound() {
Account account = new Account();
account.setUsername("Test");
account.setId(1l);
//Mockito.when(accountRepository.findById(account.getId())).thenReturn(java.util.Optional.of(account));
AccountRepository accountRepository = createNiceMock(AccountRepository.class);
expect(accountRepository.findById(account.getId())).andReturn(Optional.of(account));
replay(accountRepository);
assertEquals("Username name should be Test", account, accountService.getAccount(account.getId()));
}
Error Message:
Hibernate: select account0_.id as id1_0_0_, account0_.email as email2_0_0_, account0_.enabled as enabled3_0_0_, account0_.first_name as first_na4_0_0_, account0_.last_name as last_nam5_0_0_, account0_.password as password6_0_0_, account0_.points as points7_0_0_, account0_.settings as settings8_0_0_, account0_.username as username9_0_0_, roles1_.account_id as account_1_2_1_, role2_.id as role_id2_2_1_, role2_.id as id1_17_2_, role2_.created_by as created_5_17_2_, role2_.created_date as created_2_17_2_, role2_.last_modified_by as last_mod6_17_2_, role2_.last_modified_date as last_mod3_17_2_, role2_.name as name4_17_2_, account3_.id as id1_0_3_, account3_.email as email2_0_3_, account3_.enabled as enabled3_0_3_, account3_.first_name as first_na4_0_3_, account3_.last_name as last_nam5_0_3_, account3_.password as password6_0_3_, account3_.points as points7_0_3_, account3_.settings as settings8_0_3_, account3_.username as username9_0_3_, account4_.id as id1_0_4_, account4_.email as email2_0_4_, account4_.enabled as enabled3_0_4_, account4_.first_name as first_na4_0_4_, account4_.last_name as last_nam5_0_4_, account4_.password as password6_0_4_, account4_.points as points7_0_4_, account4_.settings as settings8_0_4_, account4_.username as username9_0_4_, privileges5_.role_id as role_id1_18_5_, privilege6_.id as privileg2_18_5_, privilege6_.id as id1_11_6_, privilege6_.description as descript2_11_6_, privilege6_.name as name3_11_6_ from account account0_ left outer join account_role roles1_ on account0_.id=roles1_.account_id left outer join role role2_ on roles1_.role_id=role2_.id left outer join account account3_ on role2_.created_by=account3_.id left outer join account account4_ on role2_.last_modified_by=account4_.id left outer join role_privilege privileges5_ on role2_.id=privileges5_.role_id left outer join privilege privilege6_ on privileges5_.privilege_id=privilege6_.id where account0_.id=?
14:50:02.707 [Test worker] WARN de.hspf.nxtgenmondial.backend.api.account.service.AccountService - Account with id 1 not found[m
javax.persistence.EntityNotFoundException
at de.hspf.nxtgenmondial.backend.api.account.service.AccountService.getAccount(AccountService.java:71)
at de.hspf.nxtgenmondial.backend.api.account.service.AccountService$$FastClassBySpringCGLIB$$62cb7fc8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at de.hspf.nxtgenmondial.backend.aop.logging.LogAspect.logTimeMethod(LogAspect.java:24)
at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at de.hspf.nxtgenmondial.backend.api.account.service.AccountService$$EnhancerBySpringCGLIB$$8f56eb16.getAccount(<generated>)
at de.hspf.nxtgenmondial.backend.api.account.service.AccountServiceTest.whenCreate_ThenRoleShouldBeFound(AccountServiceTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
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.RunBefores.evaluate(RunBefores.java:26)
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:325)
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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
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.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:412)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.lang.Thread.run(Thread.java:748)
The method is a unit test thats why I want to mock the accountRepository. I use the Spring framework, a in memory database for testing, jUnit and EasyMock for mocking the objects.
Aucun commentaire:
Enregistrer un commentaire