mercredi 27 novembre 2019

Run all test methods via reflection through @Test annotated method in java. different results

previously i had an issue with running test cases in my project which took very long because each @Test annotation creates a server to run the test method. so i wrote a method by using java reflection to run all test methods via that method.

let me explain. previously it was like this and all test methods worked fine and nothing failed.,

Class A{

    @Test
    public void testMethod1(){
        //Do something
    }

    @Test
    public void testMethod2(){
        //Do something
    }

    @Test
    public void testMethodn(){
        //Do something
    }
}

what i did was like this,

Class A{

    @Test
    public void runAllMethods(){
        Method[] methods = ((T)this).getClass().getDeclaredMethods();
        for (Method m : methods) {
         if (!(m.getName().toString()).contains("runAllMethods")) {
            try {
               m.invoke((T)(this), null);
            } catch (Exception e) {
               //
            }
         }
  }
    }

    public void testMethod1(){
        //Do something
    }


    public void testMethod2(){
        //Do something
    }


    public void testMethodn(){
        //Do something
    }
}

After i did like this. some test cases are getting fail. all the exception error are

Caused by: java.lang.AssertionError: expected:

I want to run all test methods via another @Test method. Could you please someone can help me here? Thanks





Aucun commentaire:

Enregistrer un commentaire