1

I am new to Java and I was trying to see if the scenario provided below is possible. Any reference or link to learn the below is highly appreciated. I have 4 classes.

I'm creating these classes because I'll be using them for completing a regression test on an application that will have a multitude of test that will be broken down in small classes. Sometimes those test only impact 1 page and sometimes those test will need to be run on the entire applications which is about 100 pages. Each of these pages will have a series of individual test.

Main class that runs class B and class C through class CallScripts. Similar, to how class Sleep is created.

Import package.B
Import package.C
Import static package.Sleep.sleep
Import static package.CallScript.callScript

// this is main class
Public class A {

  Public static void main(String[]  args) {
  callScript(B);
  callScript(C);
  }

}

Second class

public class B {
//do something
}

Third class

public class C {
//do something
}

Fourth class

public class CallScript{
  public static void callScript(String className) 
{
/* I am stuck here, but   
wanted to know if this 
implementation will work
*\
  }
}

I added a Sleep class, which I have working, to show the similar functionality I would like to have with callScript.

Fifth class:

public class Sleep{
  public static void sleep(int x) 
{ 
try {
  Thread.sleep(x*1000);
  } catch (InterruptedException e) 
  {
  e.printStackTrace();
  }
 }
}
Justin
  • 11
  • 2
  • 1
    What are you actually trying to accomplish? What is a "*script*"? – PM 77-1 May 26 '22 at 21:20
  • 1
    Classes don't typically "do something" - they typically contain methods which do something. So maybe you are thinking about this [How do I invoke a Java method when given the method name as a string?](https://stackoverflow.com/q/160970/12567365) But if you tell us why you want to do this in more detail, it may be easier to show you a way which avoids needing to use reflection altogether. – andrewJames May 26 '22 at 21:25
  • Thank you both so much for your comments. I have edited the question to hopefully, better describe what I am trying to do. The script is running the class in callScript. The classes have a method that has a method that is doing something. The aspect that I struggle with right now is the CallScript class. Then you again! – Justin May 26 '22 at 23:33
  • 1
    My question was more about _why_, not _what_. I remember wanting to write code like this when I was new to Java - and it turned out it was completely unnecessary. – andrewJames May 26 '22 at 23:37
  • Ohhhhh I see. I'm going to be creating hundreds of classes that i want to be able to remove or add. I might have a scenario where i have a top level class that calls a class, and potentially calls another class. I figure operation and maintenance would be easier. But i could for sure be wrong. This is me being green behind the ears. – Justin May 27 '22 at 00:00
  • 1
    OK - this may feel like peeling an onion - but _why_ do you want to create create hundreds of classes that can be added/removed? This may well be a totally legitimate use case for using reflection (as in my [earlier link](https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string)). But it may also be that you don't actually _need_ to be creating those hundreds of classes (all with different names and structures) in the first place.There may be a better/simpler way to meet your end goal. – andrewJames May 27 '22 at 00:06
  • I'm creating these classes because I'll be using them for completing a regression test on an application that will have a multitude of test that will be broken down in small classes. Sometimes those test only impact 1 page and sometimes those test will need to be run on the entire applications which is about 100 pages. – Justin May 27 '22 at 00:11
  • So sometimes I will want to call from 1 class all 100 pages or I might only need to call 20 pf those 100 pages – Justin May 27 '22 at 00:11
  • I forgot to add each of these pages will have a set of test that will need to be run. – Justin May 27 '22 at 00:14
  • 1
    You should add these details to the question. But yes, you can use reflection for this. There are other approaches which may be of interest: [How can I dynamically call methods in Java](https://stackoverflow.com/questions/25321134/how-can-i-dynamically-call-methods-in-java), [Dynamically choose method at runtime; alternatives to Visitor Pattern or Reflection](https://stackoverflow.com/questions/31527639/dynamically-choose-method-at-runtime-alternatives-to-visitor-pattern-or-reflect), and so on. – andrewJames May 27 '22 at 00:43
  • 1
    Finally, if you are using a specific (unit) test framework for your regression tests, what features does that offer? – andrewJames May 27 '22 at 00:43
  • I will add those details to question and using the Selenium Framework. – Justin May 27 '22 at 02:58

0 Answers0