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 3214464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:03:50+00:00 2026-05-17T15:03:50+00:00

My unit tests need supplementary classes, mostly as factories for creating objects of classes

  • 0

My unit tests need supplementary classes, mostly as factories for creating objects of classes under test. For example:

// EmployeeTest.java
public class EmployeeTest {
  @Test public void testName() {
    Employee emp = EmployeeTestFactory.get(); // static method
    assert(emp.getName() instanceof String);
  }
}
// SalaryTest.java
public class SalaryTest {
  @Test public void testAlwaysPositive() {
    Employee emp = EmployeeTestFactory.get(); // static method
    assert(emp.getSalary() > 0);
  }
}

As you see, I need to work with an instance of class Employee in both unit tests. I would like to create a simple factory, which will create such objects, on demand, e.g.:

public class EmployeeTestFactory {
  public static Employee get() {
    // create it, make persistent, fill with data
    // and return
  }
}

Is it a correct approach? If yes, where should I place this class? Right next to unit tests? Or maybe this factory is a sort of “resource”?

  • 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-05-17T15:03:51+00:00Added an answer on May 17, 2026 at 3:03 pm

    For me this a question of test data handling.

    There are several approaches to handle test data:

    1. Build them inside the junit classes.
      pros: you see what you have in the junit class.
      cons: You can’t reuse them.

    2. Create Factories which creates the testdata.
      pros: Corse grained reuse is guaranteed
      cons: Fine grained reuse is not possible in a good structured way (when you need testdata with distinct attribute values), you can only access your testdata inside the the project, when you build with maven.

      The builder en.wikipedia.org/wiki/Builder_pattern and the object mother pattern martinfowler.com/bliki/ObjectMother.html are useful in this case.
      Here is a comparsion: geekswithblogs.net/Podwysocki/archive/2008/01/08/118362.aspx

    3. Create Factories in an own project.
      pros: you can use your testdata in the other projects
      cons: you have an additional project to manage

    My Favorite:

    I like the most to have an own project. When the domain objects have interfaces, you can build jaxb implementations of them and hold the testdata in xml. For readability and reuse the best approach for me. You must then only say which xml testdata set to load.

    If this is overkill I like the builder pattern with the fluent interface:

     Employee.build.name("Bob").age(32).street("teststreet");
    

    Your Case:

    I would hold them under the same package name like the domain classes and add the package testdata at the end. I wouldn’t name the factory method only get. Give it a name which is meaningful.

    If you need an example how to use an inmemory database with spring, hibernate and junit, here is an example: http://little-tdd-project.origo.ethz.ch/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a heap of unit tests that need to check XML outputs. I
I've got a bunch of legacy code that I need to write unit tests
I need to use the ReSharper Unit Test Runner to run my MSTest Unit
I need to write a unit test for a method that will print a
For managing unit tests in Visual Studio, I use the Test List Editor. There's
One of our unit tests is to populate properties within our business objects with
I've made some unit tests (in test class). The tutorial I've read said that
Unit tests are cheaper to write and maintain, but they don't cover all scenarios.
I was writing some Unit tests last week for a piece of code that
I´m looking for a unit tests framework for database development. I´m currently developing for

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.