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"?