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.

4
  • Look here for static fields : stackoverflow.com/questions/3422390/… And here for static methods invocation : stackoverflow.com/questions/2467544/… Commented May 10, 2017 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. Commented May 10, 2017 at 13:04
  • With Reflection, you can get all static fields. You can find plenty of examples here on stackoverflow. e.g. stackoverflow.com/questions/2685345/… Commented May 10, 2017 at 13:04
  • Reflection is something to avoid assiduously. Commented May 10, 2017 at 17:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.