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
1342
votes
42 answers

What's the difference between a mock & stub?

I've read various articles about mocking vs stubbing in testing, including Martin Fowler's Mocks Aren't Stubs, but still don't understand the difference.
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
1181
votes
11 answers

How to mock void methods with Mockito

How to mock methods with void return type? I implemented an observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet but didn't succeed. My class looks like this: public class World…
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
964
votes
16 answers

What's the difference between faking, mocking, and stubbing?

I know how I use these terms, but I'm wondering if there are accepted definitions for faking, mocking, and stubbing for unit tests? How do you define these for your tests? Describe situations where you might use each. Here is how I use them: Fake:…
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
730
votes
13 answers

Difference between @Mock and @InjectMocks

What is the difference between @Mock and @InjectMocks in Mockito framework?
user2249972
  • 7,321
  • 3
  • 13
  • 5
696
votes
8 answers

What is Mocking?

What is Mocking?                                                                                                    .
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
601
votes
5 answers

Use Mockito to mock some methods but not others

Is there any way, using Mockito, to mock some methods in a class, but not others? For example, in this (admittedly contrived) Stock class I want to mock the getPrice() and getQuantity() return values (as shown in the test snippet below) but I want…
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
593
votes
4 answers

Mockito test a void method throws an exception

I have a method with a void return type. It can also throw a number of exceptions so I'd like to test those exceptions being thrown. All attempts have failed with the same reason: The method when(T) in the type Stubber is not applicable for the…
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83
580
votes
6 answers

Can Mockito capture arguments of a method called multiple times?

I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried: ArgumentCaptor firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor secondFooCaptor =…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
544
votes
4 answers

Returning value that was passed into a method

I have a method on an interface: string DoSomething(string whatever); I want to mock this with MOQ, so that it returns whatever was passed in - something like: _mock.Setup( theObject => theObject.DoSomething( It.IsAny( ) ) ) .Returns(…
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
497
votes
22 answers

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…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
475
votes
14 answers

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,…
Emma
  • 6,112
  • 2
  • 18
  • 11
469
votes
10 answers

How can I mock an ES6 module import using Jest?

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy -- The application code: // myModule.js import dependency from './dependency'; export default (x) => { …
Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
403
votes
7 answers

What's the best strategy for unit-testing database-driven applications?

I work with a lot of web applications that are driven by databases of varying complexity on the backend. Typically, there's an ORM layer separate from the business and presentation logic. This makes unit-testing the business logic fairly…
friedo
  • 65,762
  • 16
  • 114
  • 184
387
votes
20 answers

How can I mock requests and the response?

I am trying to use Pythons mock package to mock Pythons requests module. What are the basic calls to get me working in below scenario? In my views.py, I have a function that makes variety of requests.get() calls with different response each time def…
kk1957
  • 8,246
  • 10
  • 41
  • 63
372
votes
15 answers

How to check String in response body with mockMvc

I have simple integration test @Test public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception { mockMvc.perform(post("/api/users").header("Authorization",…
pbaranski
  • 22,778
  • 19
  • 100
  • 117
1
2 3
99 100