I have a class in Java, which has a private method for computing some default value; and two constructors, one of which omits that value and used the private method for getting the default.
public class C {
private static HelperABC getDefaultABC() {
return something; // this is a very complicated code
}
public C() {
return C(getDefaultABC());
}
public C(HelperABC abc) {
_abc = abc;
}
}
Now, I'm trying to write a test for this class, and would like to test BOTH constructors; with the second constructor being passed the default value.
Now, if getDefaultABC()
was public, that would be trivial:
// We are inside class test_C
// Assume that test_obj_C() method correctly tests the object of class C
C obj1 = new C();
test_obj_C(obj1);
HelperABC abc = C.getDefaultABC();
C obj2 = new C(abc);
test_obj_C(obj2);
However, since getDefaultABC()
is private, I can not call it from the test class!!!.
So, I'm forced to write something as stupid as:
// Assume that test_obj_C() method correctly tests the object of class C
C obj1 = new C();
test_obj_C(obj1);
// here we will insert 20 lines of code
// that are fully copied and pasted from C.getDefaultABC()
// - and if we ever change that method, the test breaks.
// In the end we end up with "HelperABC abc" variable
C obj2 = new C(abc);
test_obj_C(obj2);
Is there any way to resolve this conundrum (ideally, by somehow marking C.getDefaultABC()
as private for everyone EXCEPT for class test_C) aside from simply changing the method from private to public?