So I don't know if this is a good approach or not:
class A {
B b;
A() {
this.b = new B(this);
}
}
class B {
A a;
//* Requires the A object as a parameter */
B(A a) {
this.a = a;
}
}
The reason I did this is that the class B must hold the A object in order to operate (Think of the class A as a "holder" for class B)
However, this leads to many issue, I am not sure if this is an anti-pattern or not, but when I'm doing Unit Testing class A, it would be hard for me to mock or omit object B as there's no dependency injection.
I don't think I can implement the dependency injection pattern here as the B object NEEDS object A to operate, so passing it in as a parameters to the constructor would not work.
The only solution I have thought of is to make a setUp
method, like so:
// In the A class
public void setUp(B b) {
this.b = b;
}
This would successfully separate the testing and the actual application from themselves, while adhering to the dependency injection pattern. Though, I am not 100% sure with this approach.
I am just not sure if this approach is ideal or not.
Edit: The post that was linked to this "duplicate" one does not directly pertain to my problem. The one that was referenced got a StackOverflowError
, while mine does not, furthermore, that post's constructor does not include any parameters.
To further clarify my existing issue: my A
class is the ClientClass
, and the B
is the AccountController
class. The AccountController
handles all of the accounts of the CLIENT, and each client has one AccountController
, it handles things such as logging in and out, account creation and deletion, and track the current account. Other classes reference the ClientClass
object to also get the AccountController
, I did this mainly for readability.