简体   繁体   English

测试摘要 class 和 Mockito。怎么样?

[英]Testing abstract class with Mockito. How?

I have the following class:我有以下 class:

abstract class Foo {
        abstract List<String> getItems();
        public void process() {
            getItems()
                    .stream()
                    .forEach(System.out::println);
        }
    }

What I'd like to test is the process() method, but it is dependent on the abstract getItems() .我想测试的是process()方法,但它依赖于抽象的getItems() One solution can be to just create an ad-hoc mocked class that extends Foo and implements this getItems() .一种解决方案是创建一个临时模拟的 class 来扩展Foo并实现这个getItems()

What's the Mockito way to do that? Mockito 的方法是什么?

Why not just:为什么不只是:

    List<String> cutsomList = ....
    Foo mock = Mockito.mock(Foo.class);
    Mockito.when(mock.getItems()).thenReturn(customList);
    Mockito.when(mock.process()).thenCallRealMethod();

Or (for void)或(无效)

    doCallRealMethod().when(mock.process()).voidFunction();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito。 如何在被测类中模拟抽象方法调用? - Mockito. How to mock abstract method call in class under test? jUnit,Mockito。 如何确保抽象类中的方法(模板方法)调用抽象钩子方法? - jUnit, Mockito. How to ensure a method in abstract class (a templae method) invokes the abstract hook method? 的Mockito。验证方法参数是否为特定类 - Mockito. Verify method param to be a particular class 如何使用 Mockito 验证 PreparedStatement 场景。 无法在测试 class 中模拟 PreparedStatement class - How to validate PreparedStatement scenario with Mockito. Unable to mock the PreparedStatement class in test class Mockito 之间的区别。<Response> any().getClass() 和 Mockito。 <Class<Response> &gt; 任何()? - Difference between Mockito.<Response>any().getClass() and Mockito.<Class<Response>>any()? 的Mockito。 如何基于模拟对象作为参数返回布尔值? - Mockito. How to return a boolean value based on mocked object as parameter? java.lang.AssertionError:在Mockito中。 怎么解决? - java.lang.AssertionError: in Mockito. How to fix it? 使用 Mockito 测试抽象类不会给出预期的结果 - Testing an abstract class with Mockito does not give the expected result 莫基托。 验证方法参数 - Mockito. Verify method arguments Mockito. 没有捕获参数值 - Mockito. No argument value was captured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM