Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito mock all methods call and return

I have a problem when writing unit testing with mock. There is a object which I need to mock have a lot getter, which I do call them at the code. However, those are not the purpose of my unit test. So, is there is a way I can mock all the methods instead of mock them one by one.

Here is the code example:

public class ObjectNeedToMock{

private String field1;
...
private String field20;

private int theImportantInt;


public String getField1(){return this.field1;}
...

public String getField20(){return this.field20;}

public int getTheImportantInt(){return this.theImportantInt;}

}

and this is the service class I need to test

public class Service{

public void methodNeedToTest(ObjectNeedToMock objectNeedToMock){
    String stringThatIdontCare1 = objectNeedToMock.getField1();
    ...
    String stringThatIdontCare20 = objectNeedToMock.getField20();
    // do something with the field1 to field20

    int veryImportantInt = objectNeedToMock.getTheImportantInt();
    // do something with the veryImportantInt

    }
}

within the test class, the test method is like

@Test
public void testMethodNeedToTest() throws Exception {
      ObjectNeedToMock o = mock(ObjectNeedToMock.class);
      when(o.getField1()).thenReturn(anyString());
      ....
      when(o.getField20()).thenReturn(anyString());

      when(o.getTheImportantInt()).thenReturn("1"); //This "1" is the only thing I care

}

So, is there a way that I can avoid writing all the "when" for the useless "field1" to "field20"

like image 480
jasonfungsing Avatar asked Oct 02 '14 04:10

jasonfungsing


People also ask

What are Mockito methods in Java?

The brief description of the Mockito methods are given below: It is used to create mock objects of a given class or interface. Mockito contains five mock () methods with different arguments. When we didn't assign anything to mocks, they will return default values. All five methods perform the same function of mocking the objects.

How to test the number of invocations of a Mockito method?

The verify () method is also used to test the number of invocations. So we can test the exact number of invocations by using the times method, at least once method, and at most method for a mocked method. There are two types of verify () methods available in the Mockito class, which are given below:

What is the difference between after () and TimeOut () methods in Mockito?

The timeout () method differs from the after () method as after () method waits for the full period unless the final result is declared whereas the timeout () method will stop as soon as the verification passes. It is rarely used in testing. It allows Mockito to verify over a given period of time.

What is the use of mock () method in Java?

It takes a class or an interface name as a parameter. mock () method with Answer: It is used to create mock objects of a class or interface with a specific procedure. It is an advanced mock method, which can be used when working with legacy systems.


1 Answers

You can control the default answers of your mock. When you're creating the mock, use:

Mockito.mock(ObjectNeedToMock.class, new Answer() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
        /* 
           Put your default answer logic here.
           It should be based on type of arguments you consume and the type of arguments you return.
           i.e.
        */
        if (String.class.equals(invocation.getMethod().getReturnType())) {
            return "This is my default answer for all methods that returns string";
        } else {
            return RETURNS_DEFAULTS.answer(invocation);
        }
    }
}));
like image 99
SimY4 Avatar answered Oct 13 '22 22:10

SimY4