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
9
votes
1 answer

Creating mock Objects in PHP unit

I've searched but can't quite find what I'm looking for and the manual isn't much help in this respect. I'm fairly new to unit testing, so not sure if I'm on the right track at all. Anyway, onto the question. I have a class:
Mike
  • 695
  • 2
  • 10
  • 23
9
votes
1 answer

Registrer MappingJackson2HttpMessageConverter in Spring 3.1.2 with JAXB annotations

I have a number of entities with JAXB annotations that I would like to convert to JSON using a message-converter. I know that my ObjectMapper that reads the JAXB annotations works: String correctJsonText =…
molholm
  • 1,992
  • 3
  • 25
  • 29
9
votes
2 answers

How to unit test methods that use System.Web.Security.Membership inside?

I want to test a method to check that it saves a transaction correctly. Inside it calls Membership.GetUser() to verify the user which causes the test to fail each time. Is there any way to mock this so that Membership.GetUser() always returns a…
JohnCambell
  • 633
  • 8
  • 23
9
votes
2 answers

How do you mock a RelatedManager method in Django?

I have a customer manager for a Django model which overrides the create method to also save some related objects: class CustomManager(models.Manager): def create(self, amount, user, description): txn = self.get_query_set().create(user,…
DavidWinterbottom
  • 6,420
  • 5
  • 38
  • 39
9
votes
6 answers

Mocking and Win32 API Calls

The current product I work on is a Windows Service written in C++ and going forward all new functionality will have unit tests written for it. But this creates an interesting problem (for me at least) we do a lot of Win32 calls for various things…
user1333
9
votes
2 answers

Mocking Guid.NewGuid()

Suppose I have the following entity: public class User { public int Id { get; set; } public string Username { get; set; } public Guid UserGuid { get; set; } public Guid ConfirmationGuid { get; set; } } And the following interface…
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
9
votes
1 answer

With Rhino Mocks how to stub a method that makes use of the params keyword?

I am attempting to setup an expectation on a repository. The method makes use of the params keyword: string GetById(int key, params string[] args); The expectation I have setup: var resourceRepo =…
ahsteele
  • 26,243
  • 28
  • 134
  • 248
9
votes
1 answer

Unit testing Abstract classes in Groovy

I am new to unit testing and mocking. I am trying to unit test an abstract domain class in Grails. How should I mock an implementation so I can unit test the constraints of the domain class? Is there a way to use the mock libraries that come with…
Matthew Sowders
  • 1,640
  • 1
  • 19
  • 32
9
votes
1 answer

Mocked List using Mockito isEmpty always returns false, even if the size is 0

I'm playing with Mockito (1.9.5) and stuck at first simple test case: List mockedList = mock(ArrayList.class); assertEquals(0, mockedList.size()); // Passed assertTrue(mockedList.isEmpty()); // Failed Can anyone explain why isEmpty() here returns…
Genzer
  • 2,921
  • 2
  • 25
  • 38
9
votes
4 answers

PHPUnit: mock all methods except some

I'm writing a PHPUnit test, where I need to mock some dependency, but I need a couple of methods for it still work as before. I.e., I have: class Dependency { // some stuff not important for the test public function thisOneINeed() { /// complex…
StasM
  • 10,593
  • 6
  • 56
  • 103
9
votes
2 answers

Unit Testing - database and fixtures

I am just starting to get into unit testing and cant see an easy way to do a lot of test cases due to the interaction with a database. Is there a standard method/process for unit testing where database access (read and write) is required in order to…
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
9
votes
2 answers

How to mock the 'new' operator

I'm testing some groovy code that uses a java library and I want to mock out the library calls because they use the network. So the code under test looks something like: def verifyInformation(String information) { def request = new…
user1394451
  • 93
  • 1
  • 3
9
votes
3 answers

Best way to mock java web service

I have to mock quite complicated java web service and I'm searching for the right solution. One way to do it would be to use Soap UI, but I need something that would be able to modify server state ie. one request would affect future requests. In…
aaimnr
  • 1,646
  • 1
  • 17
  • 31
9
votes
7 answers

Is it possible to extend a class with no constructors in Java?

For unit testing purposes I'm trying to write a mock object of a class with no constructors. Is this even possible in Java, of is the class simply not extensible?
Ben S
  • 68,394
  • 30
  • 171
  • 212
8
votes
2 answers

ASP .Net MVC 3: Unit testing controller actions

I am quite new to Unit testing and Mock concepts. I am trying to figure out how to write a good test case for the basic out-of-the box user registration code below: [HttpPost] public ActionResult Register(RegisterModel model) { if…
Moon
  • 33,439
  • 20
  • 81
  • 132