0

Here's the situation. I have TestA class that creates the object of type A that needs to be tested. I also have another TestB class that contains static variable that is object of type B and runs tests on it. Testing on object A will only run if object B exists, so I currently have situation like this somwehere in TestA class:

if(BTest.objectB == null) {
    new TestB().dependencyBuild();
}

And this works. I don't wanna waste time on explaining why, but I need this snippet of code to also work if I only have the Strings with names of TestB class and objectB (or any other class my test is dependent on) given to me as arguments. I need to turn those strings into parent class and parent object variables so the above snippet rewriten like this would work:

if(ParentClass.parentObject == null) {
    new ParentClass().dependencyBuild();
}

Is something like this possible? I've already tried to use the answers here: Creating an instance using the class name and calling constructor but I don't know how to make it work when a) other class (TestB) doesn't have a constructor and b) variable I need (objectB) is static.

Community
  • 1
  • 1
Jet Black
  • 21
  • 3
  • Look here for static fields : http://stackoverflow.com/questions/3422390/retrieve-only-static-fields-declared-in-java-class And here for static methods invocation : http://stackoverflow.com/questions/2467544/invoking-a-static-method-using-reflection – Arnaud May 10 '17 at 13:04
  • 1
    Add some code to show the start conditions and assertions about the end state, until then it's not clear what you're asking. – slim May 10 '17 at 13:04
  • With Reflection, you can get all static fields. You can find plenty of examples here on stackoverflow. e.g. http://stackoverflow.com/questions/2685345/getting-value-of-public-static-final-field-property-of-a-class-in-java-via-refle – Tobi May 10 '17 at 13:04
  • Reflection is something to avoid assiduously. – Lew Bloch May 10 '17 at 17:32

0 Answers0