java - 模拟Guava CacheLoader回调方法

标签 java junit mockito guava

我一直在尝试模拟回调但没有成功。 下面是一个示例代码:

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 进行此测试的方法,否则我会这样做。

最佳答案

你的问题是你自己创建了 spy 。因此回调将与真正的方法绑定(bind)。允许 Mockito 处理对象创建可以解决这个问题。 (我使用的是mockito 1.10.19)

我重构了您的测试代码:

@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 运行程序时 - 您不需要自己初始化 Mockito。 times(1) 也是验证调用的默认值。

关于java - 模拟Guava CacheLoader回调方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958995/

相关文章:

unit-testing - 莫基托。捕获对 LocalDateTime.now() 的调用;

Java 单元测试返回参数对象

java - 为什么字段注入(inject)导致类难以测试?

java - 带有 RoboElectric 私有(private)方法的 PowerMockito 未被 mock

java - 有哪些好的开源程序可以对流行的图像文件类型进行模糊测试?

java - 静态错误和主要问题

maven-2 - 在哪里可以找到用于 EJB 测试的完整 Maven Cargo 插件示例?

java - 在 Mockito 的单元测试中使用带有 @Required 字段的类

java - 检查对服务器的所有请求的有效负载

java - Lucene 不同的结果