0

I have a method in my MainActivity (Android) and I want to mock the A instance :

public void some_method() { A a = new A(); .... } 

so I created a kind of factory class as such

public class SomeFactory(){ // some constructor public A populateWithParameter(Parameter parameter){ return new A(parameter) } } 

and the method above turns into

public void some_method(SomeFactory someFactory) { A a = someFactory.populateWithParameter(parameter); a.method_call() .... } 

I tried this

@Mock SomeFactory someFactory; public void testSomeMethod() throws Exception { SomeFactory someFactory = new SomeFactory(); when(someFactory.populateWithParameter( some_parameter)).thenReturn(null); mainActivity.some_method(someFactory); ... } 

but I get this error message

 org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); 

3 Answers 3

1

You are not mocking your factory. Also, wrong method call.

Do this instead.

 SomeFactory someFactory = mock(SomeFactory.class) when(someFactory.populateWithParameter( some_parameter)).thenReturn(null); mainActivity.some_method(someFactory); 

UPDATE

Your code has changed so for completeness this is what your test should look like. In the updated code above, you were overwriting your mock with a real object. Assuming your objects are correctly set up. Notice the different syntax for providing a return object. I think this is more readable.

@Mock SomeFactory mockFactory; @Before public void setUp() { MockitoAnnotations.initMocks(this); // set up annotated mocks } @Test public void testSomeMethod() { A subject = new A(); doReturn(subject).when(mockFactory) .populateWithParameter(any(Parameter.class)); main_activity.some_method(mockFactory); verify(mockFactory,times(1)).populateWithParameter(any(Parameter.class)); } 

Best Practices

  • When naming methods and variables, use camelCase. So main_activity becomes MainActivity, some_method becomes SomeMethod.
Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much for your response. In fact, I put thenReturn(null) just to make it simple but I need something like thenReturn(new A). So, when I do that l get an issue because after creating the object A a, there is a method call a.method_call(), but then I get java.lang.NullPointerException: Attempt to invoke virtual method "some.package.method_call" on a null object reference. Do you have an idea how to fix that ?
So in your A class there is a call to method_call? I would need to see that code too.
sorry I had some problem editing my post. It is now correct
thanks for your response: now I get this message Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter
I found on this post a workaround for my issue https://stackoverflow.com/questions/30590470/npe-when-calling-mockitoannotations-initmocks-in-androidtestcase. But then, I get NullPointerException: Attempt to invoke virtual method 'my.package.A my.factory.populateWithParameter(Parameter )' on a null object reference
|
0

You need a way to overwrite the instance of A from your test. Typically this is done using an injection framework. For a simple case, you can make the field under test protected (assuming the test class is in the same package as the class under test).

The error you are seeing comes interacting with a "real" object as if it was a mock.

In this case SomeFactory is a real object so it cannot be when()ed

Comments

0

From the point of view of response of @DiscoS2, there is indeed an issue with this declaration

MockitoAnnotations.initMocks(this); 

You should use instead SomeFactory someFactory = mock(SomeFactory.class) and then follow the updated response of @DiscoS2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.