콜백을 모의하려고 시도했습니다.Guava CacheLoader 콜백 메소드 조롱
public class TestService {
private UtilService utilService;
private LoadingCache<String, String> cache= CacheBuilder.newBuilder()
.build(new CacheLoader<String, String>() {
public String load(String key) throws Exception {
System.out.println("key = " + key);
return getKey(key);
}
});
public String getkeyFromCache(String key) throws ExecutionException {
return cache.get(key);
}
@VisibleForTesting
public String getKey(String key) {
return utilService.getKey(key);
}
}
그리고 이와 같은 테스트 케이스 : 다음은 예제 코드입니다
@RunWith(MockitoJUnitRunner.class)
public class TestServiceTest {
public static final String MYKEY = "Mykey";
@Spy
TestService testService=new TestService();
@Before
public void before() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCache() throws ExecutionException {
doReturn(MYKEY).when(testService).getKey(MYKEY);
String result=testService.getkeyFromCache(MYKEY);
String result2nd=testService.getkeyFromCache(MYKEY);
verify(testService,times(1)).getKey(MYKEY);
}
}
을하지만 그것이 내가 캐시 기능을 테스트 할
Wanted but not invoked:
testService.getKey("Mykey");
-> at utils.TestServiceTest.testCache(TestServiceTest.java:38)
However, there were other interactions with this mock:
-> at utils.TestServiceTest.testCache(TestServiceTest.java:36)
-> at utils.TestServiceTest.testCache(TestServiceTest.java:37)
at utils.TestServiceTest.testCache(TestServiceTest.java:38)
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
를 호출되지 않습니다 같습니다 하지만 UtilService는 아니기 때문에 조롱하고 싶습니다. 이 테스트에서 Answer 또는 Captor를 사용하는 방법을 생각해 보지 못했습니다.
고맙습니다. @Admit. 2 가지 문제가있었습니다. 내가 사용하고 있던 버전은 1.9.5이고 서비스의 초기화입니다. 당신이 당신의 mockito 판에 대해 나에게 말했던 좋은 물건 또는 나는 아직도보고있을 것이다. – zpontikas
예, 버전이 중요한 것으로 보입니다. 도움이 된 것을 기쁘게 생각합니다. – Admit
주어진/때/다음 테스트의 서식을 주셔서 감사합니다;) – zpontikas