File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Writing fixtures for a set of many classes in JUnit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Writing fixtures for a set of many classes in JUnit" Watch "Writing fixtures for a set of many classes in JUnit" New topic
Author

Writing fixtures for a set of many classes in JUnit

Michael Andro
Greenhorn

Joined: Sep 20, 2010
Posts: 9
Hey everyone!

I am currently developing a course scheduler for universites in Java. My team and me chose test driven development, espcially for the scheduler itself. I am using JUnit. For nearly every class there has to be JUnit test class. The database schema has become quiet big. Not everything is needed for the OR-mapped classes in the scheduler. In the end there are still 14 classes.

As the scheduler has to be highly configurable, we need classes like: courses, lecturers, students, features, rooms, roomRequiredFeatures, timeSlot, lecturerPrefferredTimeSlots, and so on. I wanted to use the @Before annotations to create fixtures (instantiated test objects to test). These test objects reflect one example of a data set from the database.

As a matter of fact, the 14 classess are not very detailed: maybe 3-4 fields. The problem arises as they are bound really tight to each other. THis results in 220 lines code of instanation in the @Before method which is way to much. A common way to deal with this is to write mockups, which simulate the test objects behaviour. But as the classes are very simple there is no way to simulate them.

I am a little bit desperate because I have the feeling our design is flawed. Are there any suggesstions or course of actions for this case?

I hope I made myself clear,

best regards!
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31077
    
233

Michael,
Have you looked at interfaces and mock objects? Not for the simple objects but for object creation. Or providing a simpler constructor for the test to use.

If you have an example of one of two of these classes, we could give more specific refactoring advice.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Michael Andro
Greenhorn

Joined: Sep 20, 2010
Posts: 9
Jeanne Boyarsky wrote:Michael,
Have you looked at interfaces and mock objects? Not for the simple objects but for object creation. Or providing a simpler constructor for the test to use.

If you have an example of one of two of these classes, we could give more specific refactoring advice.


Hey, I have read this article from IBM: http://www.ibm.com/developerworks/library/j-mocktest.html

It was interesting though I am not sure it is the right approach. Before I go into detail with a post of my classes, a question that might resolve this problem:

The data classes I want to test in JUnit have all along the same structure:

They provide class member fields and the whole class can be instantiated using one constructor. As I see yet no sense in using mock up objects, but calling the constructor like this:

new Course(1,4,null,null,null,null,"Algebra");

This means, I am leaving those values away which I will not test. This seem slike the spirit of mock up objects, yet I do not know if this is the right approach. Furthermore I got the tip tto write factory methods to produce the objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing fixtures for a set of many classes in JUnit