- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为以下方法编写一个测试:
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();
}
为此我写了下面的测试方法。当我使用 Mockito 时,模拟一切正常,但是当我想使用 EasyMock 时,我得到一个 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()));
}
错误信息:
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)
该方法是一个单元测试,这就是我要模拟 accountRepository 的原因。我使用 Spring 框架,一个用于测试的内存数据库,jUnit 和 EasyMock 用于模拟对象。
最佳答案
如果您创建一个普通的 mock,而不是一个很好的 mock,您会清楚地看到错误。匹配器似乎不匹配。
accountRepository.findById(id);
的原型(prototype)是什么? account.getId()
的返回类型是什么?
我怀疑录音中存在一些拳击问题。
关于 Spring 、JUnit 和 EasyMock : EntityNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995779/
什么情况下可以get()在 Google AppEngine 中抛出 EntityNotFoundException ? 是否真的仅在请求时数据存储中不存在所请求的 key 时发生,或者也可能由于调用
我需要一些帮助来解决这个问题。我浪费了一天的时间寻找答案却什么也没找到。 我有一个实体和模型类。实体是简单的数据,模型是实体的 Controller 。在数据库上,我有一个触发器,当发生 AFTER
我的问题很简单,但找到解决方案却成为一项相当乏味且困难的任务。 我有两个 hibernate 实体, 所有者 狗 所有者(或“父”实体)具有三个字段, 姓名 地址 dogs(Dog 实体的集合 - O
我有一个表 BusStops,它与另一个表 RouteTrip 具有外键关系。应用程序使用 Hibernate,因此这通过使用 @ManyToOne 反射(reflect)在代码中。我对 Record
我在 Symfony 和 Doctrine 问题上遇到了一个奇怪的行为,实际上发现这可能与 this 有关漏洞。 request.CRITICAL: Uncaught PHP Exception Do
我有一个表 (Records),它与另一个表 (ProgramTypes) 有外键关系。应用程序使用 Hibernate-JPA,所以这通过使用 @ManyToOne 反射(reflect)在代码中;
访问学说 2 中的代理类属性时 EntityNotFoundException 的可能原因是什么?无论如何,以下是我的实体结构: /** * @ORM\Table(name="comments")
当我尝试在 Twig 中显示相关对象时,由于 ID 在父实体中,所以该关系不存在,但相关实体不在当前数据库中,Symfony 抛出 500 错误 // EntityNotFoundException
我正在我的应用程序中处理异常。在我的服务中,我有这个方法: public Optional getEntityById(Long id) { return Optional.of(Entity
我在这里遇到了一个非常奇怪的情况。当我尝试删除一组实体并立即尝试添加另一组实体(可能会或可能不会再次具有相同的元素)时,我似乎收到以下异常。不需要整个堆栈跟踪,因为它没有说明太多内容。 javax.p
我使用 spring-data-jpa 2.1.3 和 hibernate-core 5.3.7,并且在保存具有现有父实体的子实体时,我有双向一对多关系 EntityNotFoundException
我遇到了一个在 SO 上似乎很普遍但找不到解决方案的异常。 我有两个类(class)。 文档 和关键字。每个文档都包含许多关键字。 可能使事情复杂化,这些类用于使用 Moxy 解码 xml 文档。我假
我有两个通过双向 OneToMany/ManyToOne 关系连接的实体。 @Entity class One { @OneToMany(mappedBy = "one", orphanRemov
我在创建可执行 jar 文件时遇到问题。我通过 Intellij idea X 工件创建 jar 文件。但是当我尝试执行这个 jar 时,它给了我一个错误: Exception in thread "
当用户使用无效 ID 搜索我的存储库时,我想抛出一个自定义错误类。这应该是非常直接的,但我似乎无法捕捉到 JpaRepository 抛出的任何错误。我已经多次尝试解决这个问题,但以下是我最直接的尝试
我想为以下方法编写一个测试: public Account getAccount(Long id) { log.debug("Getting account with id {}", id);
我在我的数据库中破坏了 FK,如果我加载一个实体并请求相关实体 Doctrine 将抛出 \Doctrine\ORM\EntityNotFoundException . 对于有问题的实体,我更喜欢在
我的应用程序服务中有一个方法可以接收实体的id: Document doStuff(Long documentId); 在我的方法中,我检查传递的 id 是否与之前存储的实体匹配,如果不匹配,则会抛
Exception in thread "main" javax.persistence.EntityNotFoundException: Unable to find CNPJ with id 00
我目前使用的是 Spring Boot 2.1.1.RELEASE、Hibernate Envers 5.3.7.Final。 当我查询 User 类的 AuditEntity 时,它将抛出 java
我是一名优秀的程序员,十分优秀!