运行测试用例时,我有以下异常:
org.mockito.exceptions.misusing.UnfinishedVerificationException: Missing method call for verify(mock) here:-> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)Example of correct verification: verify(mock).doSomething()Also,this error might show up because you verify either of: final/private/equals()/hashCode() methods.Those methods *cannot* be stubbed/verifIEd. at com.bignibouX.tests.repository.member.MemberCachingIntegrationTest.testFindByEmail(MemberCachingIntegrationTest.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runchild(SpringJUnit4ClassRunner.java:215) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runchild(SpringJUnit4ClassRunner.java:81) at org.junit.runners.ParentRunner.run(ParentRunner.java:238) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access@ActiveProfiles(Profiles.TEST)@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = { FullintegrationTestConfiguration.class,BaseTestConfiguration.class,MemberCachingIntegrationTest.Config.class })public class MemberCachingIntegrationTest { private static final Member MEMBER_ONE = new Member(); private static final Member MEMBER_TWO = new Member(); @autowired private MemberRepository memberRepositoryMock; @After public voID valIDate() { valIDateMockitoUsage(); } @Test public voID testFindByEmail() { when(memberRepositoryMock.findByEmail(anyString())).thenReturn(MEMBER_ONE,MEMBER_TWO); Member firstInvocation = memberRepositoryMock.findByEmail("foo@foo.com"); assertthat(firstInvocation,is(MEMBER_ONE)); Member secondInvocation = memberRepositoryMock.findByEmail("foo@foo.com"); assertthat(secondInvocation,is(MEMBER_ONE)); verify(memberRepositoryMock,times(1)).findByEmail("foo@foo.com"); Member thirdInvocation = memberRepositoryMock.findByEmail("bar@bar.com"); assertthat(thirdInvocation,is(MEMBER_TWO)); verify(memberRepositoryMock,times(1)).findByEmail("bar@bar.com"); } @Test public voID passingInexistentEmailToSendPasswordresetShouldNotCauseNPE() { fail("MemberRepository's findByEmail throws NPE if email not found in db! Reason: because cache was set up not to allow null values..."); fail("Appropriate error page not displayed when above NPE is thrown!"); } @Profile(Profiles.TEST) @Configuration static class Config { @Bean public MemberRepository memberRepositoryMock() { return mock(MemberRepository.class); } }}
0(ParentRunner.java:53) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:161) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
测试用例有两个测试,如下所示:
@RooJpaRepository(domainType = Member.class)public interface MemberRepository { @Cacheable(value = CacheConfiguration.DATABASE_CACHE_name) Member findByEmail(String email); @Cacheable(CacheConfiguration.DATABASE_CACHE_name) Member findByToken(String token); @Cacheevict(value = CacheConfiguration.DATABASE_CACHE_name,key = "#result.email")
我的问题是双重的:
>我不确定这里调用了什么:sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)…它与反射和本机方法有关但在我的情况下是哪种本机方法?
>我的Mockito用法出了什么问题?
编辑:
org.springframework.aop.framework.ProxyFactory: 2 interfaces [com.bignibou.repository.member.MemberRepository,org.mockito.cglib.proxy.Factory]; 1 advisors [org.springframework.cache.interceptor.beanfactoryCacheOperationSourceAdvisor: advice bean 'null']; targetSource [SingletonTargetSource for target object [com.bignibou.repository.member.MemberRepository$$EnhancerByMockitoWithcglib$b543cbe@4cca9ed4]]; proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; froZen=false
编辑2:
我已经调试了测试并包含了屏幕截图,这似乎证实了macias所说的:
verify(memberRepositoryMock,times(1)).findByEmail("bar@bar.com");
编辑3:另一件需要考虑的事情:如果我删除或注释掉测试中的最后一行和拆除方法,即
@Afterpublic voID valIDate() { valIDateMockitoUsage();}
和
System.out.println(memberRepositoryMock.getClass());
测试通过没有问题….
编辑4:我实际上拼命地尝试调整以下示例:https://stackoverflow.com/a/24229350/536299在另一篇文章中给我.人们注意到模拟是自动装配的,测试确实使用了d簧上下文.有人可以帮我测试一下吗?最佳答案sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) – 根据您提供的堆栈跟踪,mockito在报告错误时有点困惑.它与你的问题没有多大关系,但无论如何都是本机的NativeMethodAccessorImpl.invoke0(见grep code). SpringJUnit4ClassRunner运行它,它是如何工作的,你应该关心什么.
现在关于你真正的问题,就像我在评论中写的那样,这是因为Spring将模拟对象包装成代理.如果你添加
@ActiveProfiles(Profiles.TEST)@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = { FullintegrationTestConfiguration.class,MemberCachingIntegrationTest.Config.class })public class MemberCachingIntegrationTest { @autowired private MemberRepository cachedRepository; @autowired private MockProvIDer mockProvIDer; @After public voID valIDate() { valIDateMockitoUsage(); } @Test public voID test() { when(mockProvIDer.get().findByEmail(anyString())).thenReturn("foo","bar"); String firstInvocation = cachedRepository.findByEmail("foo@foo.com"); assertthat(firstInvocation,is("foo")); String secondInvocation = cachedRepository.findByEmail("foo@foo.com"); assertthat(secondInvocation,is("foo")); verify(mockProvIDer.get(),times(1)).findByEmail("foo@foo.com"); String thirdInvocation = cachedRepository.findByEmail("bar@bar.com"); assertthat(thirdInvocation,is("bar")); verify(mockProvIDer.get(),times(1)).findByEmail("bar@bar.com"); } @Configuration static class Config { private MemberRepository mockRepository = mock(MemberRepository.class); @Bean public MemberRepository cachedRepository() { return mockRepository; } @Bean public MockProvIDer mockProvIDer() { return new MockProvIDer(mockRepository); } } public static class MockProvIDer { private final MemberRepository repository; public MockProvIDer(MemberRepository repository) { this.repository = repository; } public MemberRepository get() { return this.repository; } }}
你会在控制台中看到它不是模拟,而是一些代理. Mockito只能发短信,这就是你收到错误的原因.
问题可能是现在,如何解决这个问题.首先,您的测试实际上不是集成测试,因为您试图模拟存储库而不是真正测试数据访问的行为方式.在这种情况下,我将退出使用d簧测试,只需简单的MockitoJUnitRunner.
更新:
好的,现在知道它实际上是要测试的缓存,我知道您需要使用Spring缓存修改模拟的存储库,并且您的测试确实是一个集成测试.
这是如何做到的.提到frant.hartm proposal的一点变化,但没有使用静态参考.我觉得有点自然.
注意:存储库返回字符串而不是成员,以获得更清晰的示例 总结
以上是内存溢出为你收集整理的java – Mockito抛出UnfinishedVerificationException(可能与本机方法调用有关)全部内容,希望文章能够帮你解决java – Mockito抛出UnfinishedVerificationException(可能与本机方法调用有关)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)