Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you provide deep mock objects?

I don't think I understand testing as well a should. I have written a lot of tests and gotten decent coverage but I cannot help feeling it has not been intuitive.

Let me explain: if I have a class the I am testing a method and it needs to be passed a big object of some sort, with all kinds of state. This object in turn contains other objects and their states that I know nothing of, how do I create mock or stub objects for this method and give it data that it can work with. It seems I have to create a big object with all kinds of internal sub object information to exercise my method. I'm clearly confused!

like image 734
treefrog Avatar asked Sep 18 '11 21:09

treefrog


People also ask

Can you use Mock objects?

Can You Use Mock Objects? Of course, before you can use mock objects, you need an application that supports mocking: a loosely coupled architecture that lets you insert a mock object to replace a real object/dependency. Some form of dependency injection facilitates this, as do most design patterns.

How do I create a mock method with a mocksettings argument?

A MockSettings object is instantiated by a factory method as follows: That setting object will be used in the creation of a new mock: Similar to the preceding section, we will invoke the add method of a MyList instance and verify that a mock method with a MockSettings argument works as it is meant to by using the following code snippet:

How do I create a mock in a list?

A MockSettings object is instantiated by a factory method as follows: MockSettings customSettings = withSettings ().defaultAnswer (new CustomAnswer ()); That setting object will be used in the creation of a new mock: MyList listMock = mock (MyList.class, customSettings);

What are mocks in Python?

Mocks – Loosely speaking, the term is used in this article to mean anything that isn’t real data or objects. Real (.*) – Fragments that return real data in the system. Admittedly, I caught myself doing what I’m about to call out (but corrected myself swiftly).


1 Answers

The other answers here are pointing you to mocking frameworks, which you should definitely look at if you're not already using (use Mockito!). However, this is almost certainly an instance of your tests telling you that you've got design problems. If you find yourself having to provide all kinds of unrelated information and mock objects just to make a test pass, then you're

  1. trying to test too many pieces at once,
  2. writing a test that will be very difficult to read and understand when you're done with it because it's impossible to determine what the test is supposed to be focused on due to a low signal/noise ratio, and/or
  3. writing an extremely fragile test that will break on the slightest, unrelated change, incurring high maintenance costs and a "just make the test pass" mentality that doesn't care what the test is supposed to be testing.

These are all symptoms of a system not designed for testability, which almost universally equates to a system not designed for readability, meaning it's not designed well.

If you care about testing well, embrace test-first thinking and TDD. I highly recommend a couple of books on the subject: "xUnit Test Patterns", which I've read and reviewed, and "Growing Object-Oriented Software, Guided by Tests", which I'm almost finished reading.

like image 199
Ryan Stewart Avatar answered Oct 05 '22 17:10

Ryan Stewart