This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing fixtures for a set of many classes in JUnit

 
Michael Andro
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Andro
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic