Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:08:58+00:00 2026-06-12T18:08:58+00:00

My question will be at the hand of a simplified example (in Java): public

  • 0

My question will be at the hand of a simplified example (in Java):

public class VehicleRepository {
  // DAOs responsible for fetching objects from data store (injected)
  private IChassisDAO chassisDAO;
  private IEngineDAO engineDAO;
  private IWheelDAO wheelDAO;
  private IAccessoryDAO accessoryDAO;

  public Vehicle getLuxuryCar() {
    VehicleBuilder builder = getVehicleBuilder();
    builder.setChassis(chassisDAO.findBySize(ChassisSize.NORMAL));
    builder.setEngine(engineDAO.findByPower(EnginePower.HIGH));
    builder.setWheelTemplate(wheelDAO.findBySize(WheelSize.NORMAL));
    builder.setAccessories(accessoryDAO.findByType(Accessory.LUXURY));
    return builder.build();
  }

  public Vehicle getOffroadCar() {
    VehicleBuilder builder = getVehicleBuilder();
    builder.setChassis(chassisDAO.findBySize(ChassisSize.LARGE));
    builder.setEngine(engineDAO.findByPower(EnginePower.HIGH));
    builder.setWheelTemplate(wheelDAO.findBySize(WheelSize.LARGE));
    return builder.build();
  }

  // Similar operations ...
}

How would you go about unit testing this class?
I’ve thought on this for a bit, and I’ve identified a few options:

Test against the returned Vehicle without mocking the builder
From an interface perspective this is the best option, but it won’t be an isolated unit test on VehicleRepository. I can’t see any real advantage to testing the VehicleRepository along with the VehicleBuilder as opposed to only testing the VehicleBuilder. The VehicleBuilder might also have complex logic (such as checks against which Accessories are present), which leads to multiple execution paths and which would make VehicleRepository’s test very complex.

Refactor the code in order to test the state of the builder
An example would be splitting the getLuxuryCar operation into two operations:

public Vehicle getLuxuryCar() {
  VehicleBuilder builder = getLuxuryCarBuilder();
  return builder.build();
}

// Visible for testing in the same package
protected VehicleBuilder getLuxuryCarBuilder() {
  VehicleBuilder builder = getVehicleBuilder();
  builder.setChassis(chassisDAO.findBySize(ChassisSize.NORMAL));
  builder.setEngine(engineDAO.findByPower(EnginePower.HIGH));
  builder.setWheelTemplate(wheelDAO.findBySize(WheelSize.NORMAL));
  builder.addAccessory(accessoryDAO.findByType(Accessory.LUXURY));
  return builder;
}

I prefer the state testing to behaviour testing, and the test will probably be more resilient, but I’m not very fond of the resulting code (especially the getLuxuryCar() method which doesn’t really do anything).

Mock the VehicleBuilder and test the behaviour
I believe that such a unit test will just be a replica of the actual operation being tested, and won’t add any real benefit. It would also be very fragile and completely dependent on the internals of the operation being tested. A likely unit test (using JMock) would be something like:

@Test
public void shouldBuildLuxuryCar() {
  context.checking(new Expectations() {{
    oneOf(chassisDAOmock).findBySize(ChassisSize.NORMAL);
    oneOf(vehicleBuilderMock).setChassis(with(any(Chassis.class)));

    oneOf(engineDAOmock).findByPower(EnginePower.HIGH);
    oneOf(vehicleBuilderMock).setEngine(with(any(Engine.class)));

    oneOf(wheelDAOmock).findBySize(WheelSize.NORMAL));
    oneOf(vehicleBuilderMock).setWheelTemplate(with(any(Wheel.class)));

    oneOf(accessoryDAOmock).findByType(Accessory.LUXURY));
    oneOf(vehicleBuilderMock).setAccessories(with(any(Set.class)));

    oneOf(vehicleBuilderMock).build();
  }});

  context.assertIsSatisfied();
}

Don’t unit test it
I’m leaning towards this option as I’m not happy with any of the alternatives, and as the operations don’t seem to really require any testing. To clarify, I’ll still unit test the VehicleBuilder in isolation, but not the VehicleRepository. This doesn’t seem to be the prevailing opinion however, as can be seen for example with this question: Unit Testing – What not to test

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:08:59+00:00Added an answer on June 12, 2026 at 6:08 pm

    I think your points all have merit, and this is merely a recommendation of what I would do.

    An integration test would be more appropriate for this code, using injected DAO objects that generate mock data or have a test data source.

    However your question is specific to unit testing, in which case I think testing against the returned vehicle object (without mocking the builder) is be the best option. To avoid complexity I would keep to simple tests on the returned vehicle (check it is not null and perhaps its’ class invariants) as opposed to details about the type of vehicle (this testing is left to the builder/producer of the vehicle).

    This advantages of the test would be:

    1. If the vehicle/builder is later extended to require additional properties (e.g. all vehicles should have brakes), but the developer neglects to update the repository, the test will fail.
    2. It will act as a sanity test ensuring, at least, the code executes without exception.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Late edit: This question will hopefully be obsolete when Java 7 comes, because
I know this question will be closed because I studied many example of this
This is my first question. I gave up and will use a hand rolled
This question will be a little longer and I am sorry for that :)
This question will be short and sweet. I know an instruction can occur between
I know that my question will be very similar to other ones already asked
Sorry if this question will sound too chaotic, feel free to edit it. I
I guess this question will sound familiar, but I am yet another programmer baffled
I hope this question will not be rejected as there is no code. But
My guess is that this question will fall under the category of duh, but,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.