Junit4 provides three method sorters org.junit.runners.MethodSorters.NAME_ASCENDING and org.junit.runners.MethodSorters.JVM and org.junit.runners.MethodSorters.DEFAULT . Normally Junit recommends you dont sort your tests that way it can run randomly and I 100% agree to that for unit tests.
But I had a requirement to write selenium tests that would first register a user and then run bunch of tests like add file, move file and copy file. Now each of these methods have to run in the order. So I started naming methods like
test10Registration
test11AddFile
test12MoveFile
But soon this started looking odd method naming convention so I wanted something where I can define my own order. It seems it was easy and all I needed was to create a custom runner. So I created this OrderedRunner and an annotation and then annotated my test methods with the new annotation and had my test class annotated with RunWith and asked it to use this runner.
public class OrderedRunner extends BlockJUnit4ClassRunner {
public OrderedRunner(Class klass) throws InitializationError {
super(klass);
}
@Override
protected List computeTestMethods() {
List list = super.computeTestMethods();
List copy = new ArrayList(list);
Collections.sort(copy, new Comparator() {
public int compare(FrameworkMethod o1, FrameworkMethod o2) {
SeleniumMethodOrder order1 = o1.getMethod().getAnnotation(SeleniumMethodOrder.class);
SeleniumMethodOrder order2 = o2.getMethod().getAnnotation(SeleniumMethodOrder.class);
if (order1.order() == order2.order()) {
return 0;
} else if (order1.order() > order2.order()) {
return 1;
} else {
return -1;
}
}
});
return copy;
}
}
and I created my own annotation
@java.lang.annotation.Target(value = { java.lang.annotation.ElementType.METHOD })
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface SeleniumMethodOrder {
public abstract int order() default 1;
}
now I can annotate my classes with
@RunWith(OrderedRunner.class)
public class StorageServiceWdTest extends WebDriverTestBase {
@Test
@SeleniumMethodOrder(order = 1)
public void testAddFile() {
logger.info("adding file");
}
@Test
@SeleniumMethodOrder(order = 2)
public void testMoveFile() {
logger.info("moving file");
}
}
But I had a requirement to write selenium tests that would first register a user and then run bunch of tests like add file, move file and copy file. Now each of these methods have to run in the order. So I started naming methods like
test10Registration
test11AddFile
test12MoveFile
But soon this started looking odd method naming convention so I wanted something where I can define my own order. It seems it was easy and all I needed was to create a custom runner. So I created this OrderedRunner and an annotation and then annotated my test methods with the new annotation and had my test class annotated with RunWith and asked it to use this runner.
public class OrderedRunner extends BlockJUnit4ClassRunner {
public OrderedRunner(Class klass) throws InitializationError {
super(klass);
}
@Override
protected List
List
List
Collections.sort(copy, new Comparator
public int compare(FrameworkMethod o1, FrameworkMethod o2) {
SeleniumMethodOrder order1 = o1.getMethod().getAnnotation(SeleniumMethodOrder.class);
SeleniumMethodOrder order2 = o2.getMethod().getAnnotation(SeleniumMethodOrder.class);
if (order1.order() == order2.order()) {
return 0;
} else if (order1.order() > order2.order()) {
return 1;
} else {
return -1;
}
}
});
return copy;
}
}
and I created my own annotation
@java.lang.annotation.Target(value = { java.lang.annotation.ElementType.METHOD })
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface SeleniumMethodOrder {
public abstract int order() default 1;
}
now I can annotate my classes with
@RunWith(OrderedRunner.class)
public class StorageServiceWdTest extends WebDriverTestBase {
@Test
@SeleniumMethodOrder(order = 1)
public void testAddFile() {
logger.info("adding file");
}
@Test
@SeleniumMethodOrder(order = 2)
public void testMoveFile() {
logger.info("moving file");
}
}
please specify which import you are doing.
ReplyDeleteSecond thing comparator takes only Object as arguements your method has FrameworkMethod as args.
So it gives error.
NIce efforts but it seems incomplete can you provide whole source code.