运行测试用例时,我有以下异常:
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$1.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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)
测试用例有两个测试,如下所示:
@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);
}
}
}
我的问题是双重的:
>我不确定这里调用了什么:sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)…它与反射和本机方法有关但在我的情况下是哪种本机方法?
>我的Mockito用法出了什么问题?
编辑:
@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")
编辑2:
我已经调试了测试并包含了屏幕截图,这似乎证实了macias所说的:
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$$1b543cbe@4cca9ed4]]; proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false
编辑3:另一件需要考虑的事情:如果我删除或注释掉测试中的最后一行和拆除方法,即
verify(memberRepositoryMock,times(1)).findByEmail("bar@bar.com");
和
@After
public void validate() {
validateMockitoUsage();
}
测试通过没有问题….
编辑4:我实际上拼命地尝试调整以下示例:https://stackoverflow.com/a/24229350/536299在另一篇文章中给我.人们注意到模拟是自动装配的,测试确实使用了弹簧上下文.有人可以帮我测试一下吗?
最佳答案
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) – 根据您提供的堆栈跟踪,mockito在报告错误时有点困惑.它与你的问题没有多大关系,但无论如何都是本机的NativeMethodAccessorImpl.invoke0(见grep code). SpringJUnit4ClassRunner运行它,它是如何工作的,你应该关心什么.
原文链接:https://www.f2er.com/spring/432779.html现在关于你真正的问题,就像我在评论中写的那样,这是因为Spring将模拟对象包装成代理.如果你添加
System.out.println(memberRepositoryMock.getClass());
你会在控制台中看到它不是模拟,而是一些代理. Mockito只能发短信,这就是你收到错误的原因.
问题可能是现在,如何解决这个问题.首先,您的测试实际上不是集成测试,因为您试图模拟存储库而不是真正测试数据访问的行为方式.在这种情况下,我将退出使用弹簧测试,只需简单的MockitoJUnitRunner.
更新:
好的,现在知道它实际上是要测试的缓存,我知道您需要使用Spring缓存修改模拟的存储库,并且您的测试确实是一个集成测试.
这是如何做到的.提到frant.hartm proposal的一点变化,但没有使用静态参考.我觉得有点自然.
@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;
}
}
}
注意:存储库返回字符串而不是成员,以获得更清晰的示例