0

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);
Eitel Dagnin
  • 959
  • 4
  • 24
  • 61

1 Answers1

0

You can use @Order annotation to set the priority in JUnit5.

@TestMethodOrder(OrderAnnotation.class)
public class JUnit5TestOrder { 
 
        @Test
        @Order(1)
        public void Testcase_3() {
                System.out.println("Testcase_3 executes");
        }
 
        @Test
        @Order(2)     
        public void Testcase_1() {
                System.out.println("Testcase_1 executes");
        }
      
        @Test
        @Order(3)
        public void Testcase_2() {
                System.out.println("Testcase_2 executes ");     
        } 
}

And in JUnit4 you can use @FixedMethodOrder to order the test cases. For more details check out JUnit Test Execution Order.

@FixMethodOrder(MethodSorters.DEFAULT)
public class JUnit4TestOrder { 
@Test
        public void Testcase_3() {
                System.out.println("Testcase_3 executes");
        }
     @Test
     public void Testcase_1() {
                System.out.println("Testcase_1 executes");
        }
     @Test
      public void Testcase_2() {
                System.out.println("Testcase_2 executes ");     
} }
MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • Thank you for the reply. Just want to know, how do I "invoke" the child class with the test annotations? Assigning the order is exactly what I intend on doing, but how do I actually go about "executing" the test class file with the order of these methods? I have updated my question with the function I have to invoke the executeTest method – Eitel Dagnin Jan 27 '21 at 06:42
  • I'm not sure why you are using reflection to execute tests. However `maven`, `ant` or even `gradle` has their own aesthetic way to execute test. – MD Ruhul Amin Jan 27 '21 at 06:53