How to build an instance in Mockito

I have a piece of code that I want to check with Mockito:

mockedClass instanceof SampleInterface 

mockedClass makes fun of the abstract class: mockedClass , and SampleInterface is the interface. This is the missing point:

 Validate.isTrue(mockedClass instanceof SampleInterface, "The mockedClass is not a SampleInterface"); 

How to mock this code?

+6
source share
2 answers

It looks like you need MockSettings.extraInterfaces .

 MockedClass mockedClass = mock(MockedClass.class, withSettings().extraInterfaces(SampleInterface.class)); 

Please note that it comes with its own warning label:

This cryptic function should be used very rarely. An object under testing should know its co-authors and dependencies exactly. If you use it often than [sic], make sure that you are really creating simple, clean, and readable code.

Alternatively, create an interface for testing that extends all the interfaces you want your layout to implement and mock this normal way.

+15
source

In addition to another answer:

If possible, you should instead wash over the interface, which means creating a layout like this:

 SampleInterface mockedClass = mock(SampleInterface.class); // not mock(MockedClass) 
0
source

Source: https://habr.com/ru/post/980035/


All Articles