10

I am just starting with Mockito and I just want to do something like :

public class Test { public void clearList(List l){ doVeryLOOOONGDatabaseCallll(); l.clear(); return; } } /// ... Test test = mock(Test.class); Mockito.when(test.clearList(any(List.class))).then( l => l.clear()); 

Have some hint to do the trick? Thank you for your help!

4
  • No, I want to clear the list put in parameters when the method clearList(List l) is called. Commented May 29, 2013 at 16:51
  • 1
    Mock doVeryLOOOONGDatabaseCallll() not clearList(). Mocking isn't about changing behavior inside methods; it's making it so you don't need external dependencies. Commented May 29, 2013 at 16:54
  • Thank you Brian, but it is a static method, I just wanted to simplify the things. I can't mock it. Commented May 29, 2013 at 16:57
  • Fair enough - didn't have that information :) Commented May 29, 2013 at 17:02

1 Answer 1

24

Something like this should do it (not tested):

doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); List<?> list = (List<?>) args[0]; list.clear(); return null; } }).when(test).clearList(any(List.class)); 
Sign up to request clarification or add additional context in comments.

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.