"JUnit4.10 Source Code Analysis" 6.1 Sorting and filtering

Source: Internet
Author: User
Tags object object

Abstract class Parentrunner<t> extends Runner implements Filterable,sortable

This section describes sorting and filtering. (Although JUnit4.8.2 source code analysis-6.1 Sorting and filtering demonstrates the way customers use sorting and filtering, some are unclear about their design intent, but it's good to read the source code first.) It might be clear to look at it. )

org.junit.runner.manipulation Bag

Sort and filter the related types in the org.junit.runner.manipulation package .

1. For example, sortable is a function interface, yqj2065 think Sortable.sort (Sorter) rather than sortable. Setsorter (Sorter).

Package Org.junit.runner.manipulation;public interface Sortable {public void sort (Sorter Sorter);}
2.Filterable similar, defines the void filter (Filter filter) throws Notestsremainexception;

3.NoTestsRemainException complains that filter filters out all the tests.


4.Sorter implements Comparator<description>. We know that theJava.util.Comparator interface is a policy class that defines the int compare (t O1, T O2) method. And sorter is a proxy role for a simple agent model . Sorter (Comparator<description>comparator) initializes the Realsubject role through constructor injection, and the test programmer can define its own sequencer. Also, Sorter applies the null object pattern , which defines the null static inner subclass.

Package Org.junit.runner.manipulation;import Java.util.comparator;import Org.junit.runner.description;public class Sorter implements comparator<description> {/** * NULL is a <code>Sorter</code> this leaves elements in a n Undefined order */public static Sorter null= new Sorter (new comparator<description> () {public int compare (descript Ion O1, Description O2) {return 0;}});
public void apply (Object object) {if (object instanceof sortable) {sortable sortable = (sortable) object;sortable.sort (thi s);}}}
Sorter method apply (Object object), which applies this to object.

5.Filter is an abstract class , and when some tests need to be filtered, test programmers define their own filters. The filter applies a Null object pattern , which defines the all static inner subclass. Static factory generates public static Filter matchmethoddescription (final Description desireddescription). The Public filter intersect (Final filter second) allows multiple filter strings to be connected.

According to the above examples, we can write our own filters.

Package Sortfilter;import Java.util.arrays;import Java.util.hashset;import java.util.set;import Org.junit.runner.description;import org.junit.runner.manipulation.filter;/** * Excludes the specified method. * @author yqj2065 */public class Methodnamefilter extends Filter {    private final set<string> excludedmethods = n EW hashset<> ();    Public Methodnamefilter (String ... excludedmethods) {        this.excludedMethods.addAll (arrays.aslist ( Excludedmethods));    }    @Override Public    Boolean shouldrun (Description Description) {        String methodName = Description.getmethodname () ;        Return!excludedmethods.contains (MethodName);    }    @Override public    String describe () {        return This.getclass (). Getsimplename () + "-excluded methods:"                + excludedmethods;}    }
UNIT4 have test methods A (), @Ignore B (), C () and D (), etc.,

Package Demo;import static tool. Print.*;import Org.junit.runner.manipulation.notestsremainexception;import Org.junit.runner.notification.runnotifier;import Org.junit.runners.blockjunit4classrunner;import Org.junit.runners.model.initializationerror;import sortfilter.methodnamefilter;import units. unit4;/** * * @author yqj2065 */public class Filterdemo {public    static void Main (String ... args) {        try {            Bloc Kjunit4classrunner runner = null;            try {                runner = new Blockjunit4classrunner (unit4.class);            } catch (Initializationerror e) {            }            Runner.filter (New Methodnamefilter ("A", "B", "C", "D"));//Exclude All            Runner.run (New Runnotifier ());        } catch ( Notestsremainexception ex) {            PLN (ex);}}    }
Output: Org.junit.runner.manipulation.NoTestsRemainException

@ignore B () is retained, although no tests are performed, but no exception is thrown.

Parentrunner<t> and Sorting and filtering



"JUnit4.10 Source Code Analysis" 6.1 Sorting and filtering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.