Questions tagged [mocking]

Mocking and faking are ways to isolate code or components to ensure that unit tests run against the testable unit of code only without actually utilizing other components or dependencies of an application. Mocking differs from faking in that a mock can be inspected to assert the results of a test.

Mocking and faking are ways to isolate code or components to ensure that s run against the testable unit of code only without actually utilizing other components or dependencies of an application. Mocking differs from faking in that a mock can be inspected to assert the results of a test.

Reasons for use

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an actual object has any of the following characteristics, it may be useful to use a mock object in its place:

  • The object supplies non-deterministic results (e.g., the current time or the current temperature);
  • It has states that are difficult to create or reproduce (e.g., a network error);
  • It is slow (e.g., a complete database, which would have to be initialized before the test);
  • It does not yet exist or may change behavior;
  • It would have to include information and methods exclusively for testing purposes (and not for its actual task).

For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.

Most commonly, isolation frameworks are used to dynamically build a mock, such as:

15654 questions
355
votes
4 answers

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…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
347
votes
14 answers

What's the best mock framework for Java?

What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?
Josh Brown
  • 52,385
  • 10
  • 54
  • 80
331
votes
12 answers

Verify object attribute value with mockito

I have a method call which I want to mock with mockito. To start with I have created and injected an instance of an object on which the method will be called. My aim is to verify one of the object in method call. Is there a way that mockito allows…
Priyank
  • 14,231
  • 18
  • 78
  • 107
328
votes
4 answers

Python mock multiple return values

I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input. Here is my code for reference. def…
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
305
votes
5 answers

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 list =…
Arthur Maltson
  • 5,760
  • 4
  • 30
  • 33
295
votes
5 answers

How to tell a Mockito mock object to return something different the next time it is called?

So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems…
DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
294
votes
11 answers

How do I mock an open used in a with statement (using the Mock framework in Python)?

How do I test the following code with unittest.mock: def testme(filepath): with open(filepath) as f: return f.read()
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
292
votes
4 answers

Asserting successive calls to a mock method

Mock has a helpful assert_called_with() method. However, as far as I understand this only checks the last call to a method. If I have code that calls the mocked method 3 times successively, each time with different parameters, how can I assert these…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
269
votes
10 answers

Mocking python function based on input arguments

We have been using Mock for python for a while. Now, we have a situation in which we want to mock a function def foo(self, my_param): #do something here, assign something to my_result return my_result Normally, the way to mock this would…
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
262
votes
8 answers

Mocking Extension Methods with Moq

I have a pre-existing Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this interface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface…
Russell Giddings
  • 8,731
  • 5
  • 34
  • 35
254
votes
12 answers

Using Mockito to test abstract classes

I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How?
ripper234
  • 222,824
  • 274
  • 634
  • 905
244
votes
23 answers

Trying to mock datetime.date.today(), but not working

Can anyone tell me why this isn't working? >>> import mock >>> @mock.patch('datetime.date.today') ... def today(cls): ... return date(2010, 1, 1) ... >>> from datetime import date >>> date.today() datetime.date(2010, 12, 19) Perhaps someone could…
Belmin Fernandez
  • 8,307
  • 9
  • 51
  • 75
242
votes
5 answers

Unfinished Stubbing Detected in Mockito

I am getting following exception while running the tests. I am using Mockito for mocking. The hints mentioned by Mockito library are not helping. org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: ->…
Royal Rose
  • 2,531
  • 2
  • 11
  • 5
241
votes
11 answers

How can I "sleep" a Dart program

I like to simulate an asynchronous web service call in my Dart application for testing. To simulate the randomness of these mock calls responding (possibly out of order) I'd like to program my mocks to wait (sleep) for a certain period of time…
Vinnie
  • 12,400
  • 15
  • 59
  • 80
231
votes
4 answers

Using Mockito's generic "any()" method

I have an interface with a method that expects an array of Foo: public interface IBar { void doStuff(Foo[] arr); } I am mocking this interface using Mockito, and I'd like to assert that doStuff() is called, but I don't want to validate what…
ripper234
  • 222,824
  • 274
  • 634
  • 905