Mockito. Verify method arguments

I’ve googled about this, but didn’t find anything relevant. I’ve got something like this: Object obj = getObject(); Mockeable mock= Mockito.mock(Mockeable.class); Mockito.when(mock.mymethod(obj )).thenReturn(null); Testeable testableObj = new Testeable(); testableObj.setMockeable(mock); command.runtestmethod(); Now, I want to verify that mymethod(Object o), which is called inside runtestmethod(), was called with the Object o, not any other. But I always … Read more

Can Mockito stub a method without regard to the argument?

I’m trying to test some legacy code, using Mockito. I want to stub a FooDao that is used in production as follows: foo = fooDao.getBar(new Bazoo()); I can write: when(fooDao.getBar(new Bazoo())).thenReturn(myFoo); But the obvious problem is that getBar() is never called with the same Bazoo object that I stubbed the method for. (Curse that new … Read more

Using Mockito to mock classes with generic parameters

Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo<T> which I need to pass into a method that expects a Foo<Bar>. I can do the following easily enough: Foo mockFoo = mock(Foo.class); when(mockFoo.getValue).thenReturn(new Bar()); Assuming getValue() returns the generic type T. But that’s going … Read more

Mockito : how to verify method was called on an object created within a method?

I am new to Mockito. Given the class below, how can I use Mockito to verify that someMethod was invoked exactly once after foo was invoked? public class Foo { public void foo(){ Bar bar = new Bar(); bar.someMethod(); } } I would like to make the following verification call, verify(bar, times(1)).someMethod(); where bar is … Read more

Using Mockito with multiple calls to the same method with the same arguments

Is there a way to have a stubbed method return different objects on subsequent invocations? I’d like to do this to test nondeterminate responses from an ExecutorCompletionService. i.e. to test that irrespective of the return order of the methods, the outcome remains constant. The code I’m looking to test looks something like this. // Create … Read more

Mockito: Trying to spy on method is calling the original method

I’m using Mockito 1.9.0. I want mock the behaviour for a single method of a class in a JUnit test, so I have final MyClass myClassSpy = Mockito.spy(myInstance); Mockito.when(myClassSpy.method1()).thenReturn(myResults); The problem is, in the second line, myClassSpy.method1() is actually getting called, resulting in an exception. The only reason I’m using mocks is so that later, … Read more

Mocking static methods with Mockito

I’ve written a factory to produce java.sql.Connection objects: public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory { @Override public Connection getConnection() { try { return DriverManager.getConnection(…); } catch (SQLException e) { throw new RuntimeException(e); } } } I’d like to validate the parameters passed to DriverManager.getConnection, but I don’t know how to mock a static method. I’m using … Read more