cgf cgf - 1 year ago 74
Java Question

How do I build a multi-layer test object?

I have some classes I want to test, but I find the underlying entities very complex from a testing perspective since entites can have many layers of dependencies.

For example, I have a class called

, which has a
, which has
s, which have
s, and then a function which takes a building as a parameter and checks if there are more diesel cars than petrol. That means that in my tests I need to create a
, then attach a
to it, to which I have to attach a
and so on.

Is there any standard way of dealing with deeply nested entities?

Answer Source

If you're testing a function that requires a multiple of some type of object, i.e. a List, to verify the counting, there's a few ways to go about it.

One way is just mocking a bunch of objects to return the value. You should be mocking at each individual level, so at the Building level, you'd be mocking Parking, and at the Parking level, you'd be mocking Cars.


However, you can also isolate for a unique case by using seed value creation if you want a less mock-everything approach.

public class CarTest {

    private EngineType DIESEL = EngineType.DIESEL;

    public void testCreate() {
        Car car = createWithSeedValue();
        Assert.equals(DIESEL, car.getEngine().getEngineType());

    public Car createWithSeedValue() {
        return create(EngineTest.createWithSeedValue(DIESEL));

    public Car create(Engine engine) {
        Car car = new Car(engine);
        return car;

In this case, instead of creating a bunch of mock Cars, we can fill a garage with a bunch of real Car POJOs of our own definition, providing only the seed values we need for the test to work with actual objects. The benefit of this is that you can more easily instantiate Lists with actual POJOs than have to mock individual objects being passed into the list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download