IsaacLevon
IsaacLevon

Reputation: 2570

Testing abstract class with Mockito. How?

I have the following 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(). One solution can be to just create an ad-hoc mocked class that extends Foo and implements this getItems().

What's the Mockito way to do that?

Upvotes: 2

Views: 3224

Answers (1)

Rob Audenaerde
Rob Audenaerde

Reputation: 20029

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();

Upvotes: 2

Related Questions