Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock An Interface Java PowerMockito

I m trying to mock an interface.

public interface FlowCopyParamsBusinessManager {
List<FlowCopyParams> findByAppli(String application, String sourcePattern)
        throws FlowCopyParamsBusinessException;

}

In my code, when i call this method findByAppli, i would like to return a list of FlowCopyParams.

List<FlowCopyParams> lstFlowCopyParams = flowCopyParamsBusinessManager.findByAppli(
                    "TOTO","TATA);

Here my try in the class test:

@BeforeClass
public static void mockBeanIn() throws Exception {
List<FlowCopyParams> flowCopyParamsList = new ArrayList<>();

PowerMockito.spy(FlowCopyParamsBusinessManager.class);
PowerMockito.when(FlowCopyParamsBusinessManager.class, "findByAppli",  Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);
}  

I have this error :

java.lang.IllegalArgumentException: object is not an instance of declaring class

I don't know why because the method findByAppli must have two string parameters, and i put Mockito.anyString() and i still have IllegalArgumentException.

Any clue ?

Thxs.

like image 896
Kikou Avatar asked Sep 01 '16 09:09

Kikou


People also ask

Can you mock an interface Java?

mock() The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.

How do you inject an mock for an interface?

Here is what works: public class TestDo { @Mock private Do do; @Mock private ABC abc; @Before public void init() { MockitoAnnotations. initMocks(this); do. abc = abc; } @Test public void testDo() { when(do.

How do you mock an interface class?

We can use org. mockito. Mockito class mock() method to create a mock object of a given class or interface. This is really the simplest way to mock an object.

What is the difference between Mockito and PowerMockito?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.


2 Answers

You don't need to use PowerMockito, and as its an Interface, theres no need to spy() as you are not relying on any non mocked logic.

It can be done like this, in your test class define a class variable.

private FlowCopyParamsBusinessManager flowCopyParamsBusinessManagerMock;

In an @Before annotated method:

flowCopyParamsBusinessManagerMock = Mockito.mock(FlowCopyParamsBusinessManager.class);
List<FlowCopyParams> flowCopyParamsList = new ArrayList<>();
when(flowCopyParamsBusinessManagerMock 
.findByAppli(Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);

Then refer to flowCopyParamsBusinessManagerMock in your tests.

like image 92
UserF40 Avatar answered Oct 03 '22 15:10

UserF40


My test did not work because I was trying to spy the class and not on the instance of FlowCopyParamsBusinessManager.class .

First , we have to create the mock :

FlowCopyParamsBusinessManager mockFlowCopyParamsBusinessManager = PowerMockito.mock(FlowCopyParamsBusinessManager.class);

Then , spy the instance :

PowerMockito.spy(mockFlowCopyParamsBusinessManager);
PowerMockito.when(mockFlowCopyParamsBusinessManager, "findByAppli", Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);

It works as well !

like image 31
Kikou Avatar answered Oct 03 '22 16:10

Kikou