views:

29

answers:

3

My application connects to db and gets tree of categories from here. In debug regime I can see this big tree object and I just thought of ability to save this object somewhere on disk to use in test stubs. Like this:

mockedDao = mock(MyDao.class);
when(mockedDao.getCategoryTree()).thenReturn(mySavedObject);

Assuming mySavedObject - is huge enough, so I don't want to generate it manually or write special generation code. I just want to be able to serialize and save it somewhere during debug session then deserialize it and pass to thenReturn in tests. Is there is a standard way to do so? If not how is better to implement such approach?

A: 

Have look at Dynamic Managed Beans - this offers a way to change values of a running java application. Maybe there's a way to define a MBean that holds your tree, read the tree, store it somewhere and inject it again later.

Andreas_D
+1  A: 

I do love your idea, it's awesome!

I am not aware of a library that would offer that feature out of the box. You can try using ObjectOutoutStream and ObjectInputStream (ie the standard Java serialization) if your objects all implement Seriablizable. Typically they do not. In that case, you might have more luck using XStream or one of its friends.

Adrian
+1  A: 

We usually mock the entire DB is such scenarios, reusing (and implicitly testing) the code to load the categories from the DB.

Specifically, our unit tests run against an in-memory database (hsqldb), which we initialize prior to each test run by importing test data.

meriton