Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have to write unit tests for some classes my group is developing. The classes are fairly simple, and I'm not sure how to best test them.

public class MyObjectRegistry
{
    private myDataSetMapper = new MyDataSetMapper();

    public boolean StoreObjects(list<MyObject> myObjectList)
    {
        myDataSetMapper.StoreObjects(myObjectList);
    }
}

public class  MyDataSetMapper
{
    public boolean StoreObjects(list<MyObject> myObjectList)
    {
        for(MyObject myObj: myObjectList)
        {
            boolean result = StoreObject(myObj);
        }
    }

    private boolean StoreObject(MyObject myObject)
    {
        //Store the object in a database
    }
}

So my question is how to test the MyObjectRegistry.StoreObjects and MyDataSetWrapper.StoreObjects methods since there's no real logic other than a foreach loop and calling out to another method.

I've looked around the web and found that I think I'm looking at a "facade" class/method, but I'm still unclear on what would I should test/assert in the test method.

share|improve this question
1  
It's unclear exactly what you're trying to do here. Why does MyObjectRegistry even exist? What happens to result? What gets returned from StoreObjects? Does StoreObject contain all the database logic, or is there another class that does that? I think if you wrote your tests first, you'd end up with an entirely different design, but too much is missing to assert that for sure. –  pdr Nov 25 '13 at 18:49
    
Pretend you can't see the source code inside of your classes and you have no idea how they work, what would you test then to make sure the class is working as intended? –  Mike Nov 25 '13 at 18:49
add comment

1 Answer

The problem you are facing is the result of "bad" class design. Specifically MyObjectRegistry was not designed to facilitate unit testing. Luckily, reflection allows you to set private values of classes.

Here is what I would do in your situation:

  1. Create a mock version of MyObjectRegistry; probably using a List, a Map, or, perhaps, a Map of List ojects.
  2. At the start of your junit test (perhaps in class setup) create a MyObjectRegistry instance then set the myDataSetMapper member to the mock version of the MyDataSetMapper class.
  3. Call the storeObjects method then check to see that the values which were passed to the storeObjects method were added to the mocked MyObjectRegistry object.

Assuming that the database access code in not inside the MyDataSetMapper.storeObject() method, do the same thing for MyDataSetMapper with a mocked data access object.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.