Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite a settermethod in Mockito?

I can't seem to figure out how to mock a simple setter method using Mockito. I have the following class:

class MyClass {
    private SomeObject someObject;

    public void setSomeObject(SomeObject someObject) {
        this.someObject = someObject;
    }

    public someObject getSomeObject() {
        return someObject;
    }
}

Now I just want that when "setSomeObject" is called that a new instance of "SomeObject" is set. Also the parameter within the setter should be ignored.

I need something like this:

MyClass mockedClass = mock(MyClass.class);
when(mockedClass.setSomeObject([ignoreWhatsInHere]))
    .then(mockedClass.setSomeObject(new SomeObject();

However, I can't seem to get the syntax working for this. I can only get the mocks to work using getters(), because then I can return something. But I can't figure out how to do the same for setters().

All help appreciated.

like image 946
Lonelyisland Avatar asked Apr 18 '13 09:04

Lonelyisland


People also ask

Can we override setter method in Java?

You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class.

What is the return type for a setter method?

Setter returns nothing (undef) As there is no real "nothing" in Perl, this means the function needs to return undef. There are two ways to do this either by calling return undef; or by calling return; without providing anything.

What is stubbing in Mockito?

A stub is a fake class that comes with preprogrammed return values. It's injected into the class under test to give you absolute control over what's being tested as input. A typical stub is a database connection that allows you to mimic any scenario without having a real database.

What is doAnswer Mockito?

Answer is used when you need to do additional actions when a mocked method is invoked, e.g. when you need to compute the return value based on the parameters of this method call. Use doAnswer() when you want to stub a void method with generic Answer .


1 Answers

You should be able to use the doThrow()|doAnswer()|doNothing()|doReturn() family of methods to perform a suitable action when testing methods that return void including setters. So instead of

when(mockedObject.someMethod()).thenReturn(something)

you would use doAnswer() to return a custom Answer object, although it's not terribly elegant, and you might be better off using a stub:

doAnswer(new Answer() { 
    public Object answer(InvocationOnMock invocation) {
          //whatever code you want to run when the method is called
          return null;
  }}).when(mockedObject).someMethod();
}

If you are trying to return different values from the same getter call, you may also want to look at mockito's consecutive stubbing calls, which will allow you to, for example, throw an error for the first call of a method, and then return an object from the second call.

see http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#12 for more details on both of these.

like image 147
RoryB Avatar answered Sep 25 '22 18:09

RoryB