3

I have following test method which takes parameters from excel sheet. Let say I have 5 test cases, so this method will execute 5 times. But when I execute first test case (TC01) the Test() method name should change at runtime according test scrips like Test_TC01(),Test_TC02() etc.

@Test
public void Test() throws Exception {

    ExcelUtils.setExcelFile(System.getProperty("user.dir") + "\\src\\data_engine\\DataEngine.xlsx");
    DOMConfigurator.configure("log4j.xml");
    String Path_OR = System.getProperty("user.dir") + "\\src\\config\\OR.properties";
    FileInputStream fs = new FileInputStream(Path_OR);
    OR = new Properties(System.getProperties());
    OR.load(fs);

    DriverScriptTest startEngine = new DriverScriptTest();

    startEngine.execute_TestCase();

}

Please share your comments

Rahul Karankal
  • 103
  • 2
  • 13
  • Is reflection a must for your problem? Why not use factory pattern? – Developer Marius Žilėnas Mar 21 '16 at 10:02
  • change a method name? what is the use of that? ever heard of String concatenation? – Stultuske Mar 21 '16 at 10:06
  • i think he means varargs in the method signature? [ref](http://stackoverflow.com/questions/519752/java-params-in-method-signature) – Debosmit Ray Mar 21 '16 at 10:07
  • @Stultuske i'm executing multiple selenium test scripts from a single method. Now i want keep track of each method's status to include in reports. So I want to change method name dynamically as per the parameters or as per test scripts. – Rahul Karankal Mar 21 '16 at 10:17
  • @RahulKarankal one test should test one thing. create several tests. – Stultuske Mar 21 '16 at 10:20
  • @Stultuske i have created multiple tests but i'm invoking each test using single method and creating separate methods is not feasible as I am having 2700 test methods. Now my test framework is stable and gives proper results, but i just want to include the method names in reports. – Rahul Karankal Mar 21 '16 at 10:23
  • @Willmore No reflection is not must for my problem, but i don't want to create object. I just want to change method name at runtime. Let's say i have one method Test_1(), i want to change the method name to Test_2() at runtime. – Rahul Karankal Mar 21 '16 at 10:30
  • I'm not a Java professional but I think that's not possible. You would have to change the compiled code within the *.class files. Maybe you could do that for each test: look for the method name in your *.java class file, rename it to whatever you want, compile the class and run your test. – Alex Mar 21 '16 at 11:11
  • @RahulKarankal Why can't you just get the method name as a string, and concat whatever you want to add to it, and include that string in the report as the method name? – forgivenson Mar 21 '16 at 11:47
  • @forgivenson I also want the status of each test case. Please have a look at the updated question. I have mentioned more details in it. – Rahul Karankal Mar 21 '16 at 11:53

1 Answers1

2

In short, you can't.

What you can do is create a new class (at runtime!), compile it and run it.

Yes, what I'm talking about is that you write code to:

  1. Create the class (in a temp file)
  2. Use the Java Compiler API to compile the class.
  3. Call the methods on the compiled class instance.

Good luck! I've worked with this code and it's very interesting, but almost always overkill unless you really need it.

Marco
  • 8,958
  • 1
  • 36
  • 56