mockito

How to only throw exception when the mocked method is called for the first time?

I have a method of a mocked object that can be called multiple times (think recursion). The method is defined like this: public void doCommit() { } In order to tell it to fail I use this convention: doThrow(new RuntimeException()).when(mMockedObject).doCommit(); This though, makes the method throw this exception EVERY time it is ca...

Mock File class and NullPointerException

Hi. I'm creating a File mock object with Mockito that will be used as the directory to store a new File. Folder folder = Mockito.mock(File.class); File file = new Agent().createNewFile(folder, "fileName"); and inside my Agent class: public File createNewFile(File folder, String filename){ return new File(folder, "testfile"); ...

Mockito. Verify method arguments

Hello. 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 obj = new Testeable(); obj.setMockeable(mock); command.runtestmethod(); Now, I want to verify that mym...

Mocking scala object

I am using mockito and trying to mock a scala object. object Sample { } //test class SomeTest extends Specification with ScalaTest with Mockito { "mocking should succeed" in { val mockedSample = mock[Sample] } } Give me 2 compilation errors. error: Not found type Sample error: could not find implicit value for parameter...

Using Mockito, how do I intercept a callback object on a void method?

I'm using mockito to test a legacy JAAS/LDAP login module. The javax.security.auth.callback.CallbackHandler interface defines the function: void handle(javax.security.auth.callback.Callback[] callbacks) I'm expecting callbacks to contain a NameCallback, which is the object that needs to be manipulated to pass the test. Is there a wa...

Capture an argument in Mockito

I'm testing a certain class. This class is internally instantiating a "GetMethod" object that gets passed to a "HttpClient" object that gets injected into the tested class. I'm mocking the "HttpClient" class, but I would need to modify the behaviour of one method of the "GetMethod" class too. I'm playing with ArgumentCaptor but I don't ...

Can mockito or easymock replace rmock

I'm sitting with a legacy project, and we're starting to replace some old legacycode. As Rmock don't have support for junit4, we would like to replace it. One thing i was wondering is - how could i replace the dynamictestsuite feature of rmock. This is a good feature where you create a dynamic testsuite for each run, and can do stuff lik...

Mockito Passes but Code Coverage still low

package com.fitaxis.test; import java.sql.SQLException; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static org.mockito.Mockito.*; import com.fitaxis.leaderboard.LeaderBoard; public class LeaderBoardTests { @T...

Is it a right case for Mockito spy?

Let's say I have a class class SomeClass { public void methodA() {} public void methodB() {} public void someMethod() { methodA(); methodB(); } } I would like to test behavior of someMethod() with Mockito. The only way I could think of is using spy(); Something like SomeClass someClass = spy(new SomeClas...

Attempt to stub android Activity class using PowerMockito throws RuntimeException "Stub!"

I found this example where they used PowerMock and EasyMock to stub/mock the Menu and MenuItem classes for android. I have been trying to do something similar with PowerMock and Mockito with the Activity class. I understand that a lot of the methods are final and that in the Android.jar they all just throw RuntimeException("Stub!"). I...

throw checked Exceptions from mocks with Mockito

I'm trying to have one of my mocked objects throw a checked Exception when a particular method is called. I'm trying the following. @Test(expectedExceptions = SomeException.class) public void throwCheckedException() { List<String> list = mock(List.class); when(list.get(0)).thenThrow(new SomeException()); String test = list.g...

Final method mocking

Hi! I need mock some class with final method using mockito. I have wrote something like this @Test public void test() { B b = mock(B.class); doReturn("bar called").when(b).bar(); assertEquals("must be \"overrided\"", "bar called", b.bar()); //bla-bla } class B { public final String bar() { return "fail"...

Problem mocking hibernate's SessionFactory using Mockito

Any idea why the following mocking code does not work? org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); org.hibernate.Session session = Mockito.mock(Session.class); Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session); The thenReturn statement does not compile. "The method thenReturn(Se...

Mockito mocks with Spring: "Argument passed to verify() is not a mock!"

Hello, I used the code from this blog to inject Mockito mocks in my unit tests. However, before the mock is autowired it gets wrapped by Spring in a JDK proxy. This causes any verify(autowiredMock) to throw "Argument passed to verify() is not a mock!". The exception is thrown when Mockito is checking that the argument passed to verify(....

I'm trying to mock Jersey WebResource with Mockito, and can't do it...

This is my code (Jersey 1.4 + Mockito 1.8.5): import org.junit.Test; import static org.junit.Assert.*; import com.sun.jersey.api.client.WebResource; import static org.mockito.Mockito.*; public FooTest { @Test public shouldMakeAHttpCall() { WebResource wr = mock(WebResource.class); doReturn(wr).when(wr).accept(anyVararg()); ...

How should I mock Jersey HTTP-client requests?

This is the class I'm trying to test (it calculates the size of HTTP page): import javax.ws.rs.core.MediaType; import com.sun.jersey.api.client.*; public class Loader { private Client client; public Loader(Client c) { this.client = c; } public Integer getLength(URI uri) throws Exception { return c.resource(uri) // return...

JUnit mocking with Mockito, EasyMock, etc

I'm trying to mock a method of an object inside the class I'm testing. For instance class ClassToTest { public doSomething () { SomeObject a = new SomeObject (); a.doSomethingElse (); } } Is there a way to mock the methods of the variable "a"? I'd like doSomethingElse to do nothing during testing. I'm currentl...

How to check if a parameter contains two substrings using Mockito?

I have a line in my test that currently looks like: Mockito.verify(mockMyObject).myMethod(Mockito.contains("apple")); I would like to modify it to check if the parameter contains both "apple" and "banana". How would I go about this? ...

Mockito: Injecting Mocks Throughout Control Flow

I'm still learning mockito and right now I'm learning how to inject mocks. I have an object under test with a particular method that depends on other objects. Those objects, in turn, depend on other objects. I want to mock certain things and have those mocks be used everywhere during execution--throughout the control flow of the metho...