2017-10-26 3 views
1

私はコールバックを成功裏に模擬しようとしています。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を使用する方法は考えられません。

答えて

2

あなたの問題はあなた自身がスパイを作成したことです。コールバックは実際のメソッドに結び付けられます。 Mockitoがオブジェクト作成に対処できるようにして、問題を解決します。あなたはMockitoを自分で初期化する必要はありません - あなたはMockitoランナーを使用しているとして:

@RunWith(MockitoJUnitRunner.class) 
public class TestServiceTest { 

    public static final String MYKEY = "Mykey"; 
    @Spy 
    TestService testService; 

    @Test 
    public void testCache() throws ExecutionException { 
     doReturn(MYKEY).when(testService).getKey(MYKEY); 

     String result=testService.getkeyFromCache(MYKEY); 
     String result2nd=testService.getkeyFromCache(MYKEY); 

     verify(testService).getKey(MYKEY); 
    } 

} 

注:

を(私はmockito 1.10.19を使用しています)私はあなたのテストコードをリファクタリングしました。また、times(1)はコールを検証するためのデフォルト値です。

+0

2つの問題がありました。私が使っていたバージョンは1.9.5で、サービスの初期化でした。あなたがあなたのmockitoバージョンについて教えてくれたことはいいことです。それでも私はまだ見ています。 – zpontikas

+0

はい、バージョンが重要なようです。私はそれが助けてうれしいです。 – Admit

+0

与えられた/時にはテストの書式設定に感謝します;) – zpontikas

1

load()メソッド内でgetKey()メソッドが呼び出されると、mock-proxyではなくTestServiceクラス内で呼び出されます。だからこそ

doReturn(MYKEY).when(testService).getKey(MYKEY); 

は動作しません。しかし、あなたはUtilServiceを模倣して、それを参照する必要があります。

+0

答えをいただきありがとうございます。また、あなたが新しいstackoverflowであるので、コードが間違っていることを示唆することなく質問に答えるようにしてください。ほとんどの場合、実際のコードを掲載するのではなく、コード問題の例を投稿します。 – zpontikas

+0

@zpontikasありがとう、ありがとうございます@Admit。 –