Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating mock data for unit testing

Tags:

tdd

mocking

I consider myself still pretty new to the TDD scene. But find that no matter which method I use (mock framework or stubbing my own objects) I find that I have to write a lot of code to create mock data. I like the idea of loading up objects to create an in-memory database. But what I don't like is cluttering up my tests with a ton of code for the sole purpose of creating mock data. This is especially the case when the data needs to account for all the different cases.

I'd love some suggestions for a better way of doing this.

It would seem to me that I should be able to load the data once into a known state from some data store and then I could use a snapshot of that state which is loaded in the test setup/initialize before each test method is executed. This would satisfy proper testing practices while providing convenience and let me focus on writing tests instead of writing code to create test data "by hand".

like image 937
Mark J Miller Avatar asked Feb 27 '09 18:02

Mark J Miller


1 Answers

May be you could try the NBuilder library. It provides a very fluent interface and is easy to use. You can use it for generating single instances of a class with defualt values or generate lists with default or overriden values. You can have a look at this one.

like image 179
Nilesh Gule Avatar answered Sep 30 '22 12:09

Nilesh Gule