Parent Class:
@BeforeTest
public void prepareTest()
@Test
public void runTest()
@AfterMethod
public void tearDown()
@AfterTest
public void endReport()
Test case classes directory:
TestFolder
--> TestCase1
--> TestCase2
--> TestCase3
Within each of these test case class files, I have varying amount of methods, obviously depending on the test case. But in order to execute these methods, I had to create a method in every test case called executeTest. Within this method, I call all the methods in that class file in the order I need them.
public boolean executeTest()
{
myMethod1()
myMethod2()
myMethod3()
myMethod4()
return true;
}
Then, inside of my runTest method in my parent class, I have a method call to invoke the executeTest method within the child class.
This all works perfectly fine, but what I am wanting to do is get away from the method in the child classes and instead assign the @Test annotation to each method, with a priority for the the order of execution.
Something like this:
@Test(priority = 1)
myMethod1()
@Test(priority = 2)
myMethod2()
@Test(priority = 3)
myMethod3()
@Test(priority = 4)
myMethod4()
Is this possible to accomplish? Is there a better way of doing this perhaps?
Edit:
Here is the function I use to invoke the method executeTest in the child classes:
public boolean executeMethod(String className)
{
//https://stackoverflow.com/questions/18778819/dynamically-calling-a-class-method-in-java
//https://stackoverflow.com/questions/5266532/can-i-get-all-methods-of-a-class
//String methodNames[] = new String[]{"executeTest"};
String mName = "";
try {
Class classRef = Class.forName(className);
Object instance = classRef.newInstance();
Method[] methodNames = classRef.getDeclaredMethods();
for (Method methodName : methodNames)
{
mName = methodName.getName();
try
{
if(mName.equalsIgnoreCase("executeTest"))
{
Method method = classRef.getDeclaredMethod(mName);
method.invoke(instance);
}
}
catch (NoSuchMethodException | SecurityException | IllegalArgumentException ex)
{
ex.printStackTrace();
}
catch(InvocationTargetException e)
{
if(e.getCause().toString().toLowerCase().contains("TestException".toLowerCase()))
{
throw new TestException();
}
}
}
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex)
{
ex.printStackTrace();
}
return true;
}
In my parent class file, within the runTest method, I call it like this:
executeMethod(projectName + "." + TestCase);