What would be a better implementation of shared variable among subclass
- by Churk
So currently I have a spring unit testing application. And it requires me to get a session cookie from a foreign authentication source. Problem what that is, this authentication process is fairly expensive and time consuming, and I am trying to create a structure where I am authenticate once, by any subclass, and any subsequent subclass is created, it will reuse this session cookie without hitting the authentication process again.
My problem right now is, the static cookie is null each time another subclass is created. And I been reading that using static as a global variable is a bad idea, but I couldn't think of another way to do this because of Spring framework setting things during run time and how I would set the cookie so that all other classes can use it.
Another piece of information. The variable is being use, but is change able during run time. It is not a single user being signed in and used across the board. But more like a Sub1 would call login, and we have a cookie. Then multiple test will be using that login until SubX will come in and say, I am using different credential, so I need to login as something else. And repeats.
Here is a outline of my code:
public class Parent implements InitializingBean {
protected static String BASE_URL;
public static Cookie cookie;
... All default InitializingBean methods ...
afterPropertiesSet() {
cookie = // login process returns a cookie
}
}
public class Sub1 extends Parent {
@resource public String baseURL;
@PostConstruct
public void init() {
// set parents with my baseURL;
BASE_URL = baseURL;
}
public void doSomething() {
// Do something with cookie, because it should have been set by parent class
}
}
public class Sub2 extends Parent {
@resource public String baseURL;
@PostConstruct
public void init() {
// set parents with my baseURL;
BASE_URL = baseURL;
}
public void doSomethingElse() {
// Do something with cookie, because it should have been set by parent class
}
}