How to set priority for test methods execution using Annotation?
Got annotation with priority:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
int priority();
}
Got test class:
@Test(priority = 9)
public static void test(){
System.out.println("Test9");
}
@Test(priority = 8)
public static void test1(){
System.out.println("Test8");
}
@Test(priority = 7)
public static void test2(){
System.out.println("Test7");
}
}
Got method to execute with priority, but it's not working:
for (Method m : methods){
if (m.isAnnotationPresent(Test.class) && m.getAnnotation(Test.class).priority() == 9){
m.invoke(null);
}
if (m.isAnnotationPresent(Test.class) && m.getAnnotation(Test.class).priority() == 8){
m.invoke(null);
}
if (m.isAnnotationPresent(Test.class) && m.getAnnotation(Test.class).priority() == 7){
m.invoke(null);
}
}
How to execute methods with priority?
CodePudding user response:
I guess, you are using junit library for testing. You can use @Order
annotation
@Test
@Order(1)
public void firstTest() {
output.append("a");
}
@Test
@Order(2)
public void secondTest() {
output.append("b");
}
or in this documentation there are different ways to do this.