Runner.sort, Request.sortwith and sorter.apply
yqj2065 soon, they got killed.
Sorter.apply (), Request.sortwith () and Sortable.sort () do one thing? Why is it?
The Java.util.Comparator interface is a policy class that defines the int compare (t O1, T O2) method. Org.junit.runner.manipulation. Sorter implements Comparator<description>, but Sorter but the formal realization class. Sorter (Comparator<description>comparator) by constructor injection, initializes an actual sequencer in the sorter package comparator<description> Fcomparator.
and @Override public intcompare (Description O1, Description O2) returns as the actual sequencer returns.
Okay, all right. What is this design pattern? I'm not quite sure, but there's some truth in it. really do not understand is the Sorter method apply. When will this be practical? ( Note: Sortingrequest used a bit )
Publicvoid apply (Object object) { if (object instanceof sortable) { sortablesortable = (sortable) Object; Sortable.sort (this); } }
The only method that interface sortable defines is public void sort (sortersorter).
And sortable is the parent interface of some runner, that is, the sortable object is a sort of runner that performs a test.
According to my immediate intuition, Sortable.sort (Sorter) is better called Sortable.setsorter (Sorter), some runner such as Parentrunner. There should be two domains Sortstyle and Filterstyle. Rather than implements them. So easier to understand?
Now there are actual sequencer:
Package Mytest.sort;import Java.util.comparator;import Org.junit.runner.description;public class AlphabetComparator Implements comparator<description> { @Override public int compare (Description d1, Description D2) { Return D1.getmethodname (). CompareTo (D2.getmethodname ()); }
Have a test target.
Package Mytest.sort;import static tool. Print.*;import Org.junit.*;//test/ignorepublic class unit4{ @Test public void A () { PLN ("A () method executed."); } @Test @Ignore public void B () { PLN ("B () method executed."); @Test public void C () { PLN ("C () method executed."); throw new RuntimeException ("Throw delibrately"); } @Test public void F () { PLN ("f () method executed.");} }
And then what? Fennel bean anise has 3 kinds of writing? Unknown.
Package Mytest.sort;import Org.junit.runners.blockjunit4classrunner;import Org.junit.runners.model.initializationerror;import Org.junit.runner.notification.*;import org.junit.runner.*; Import Org.junit.runner.manipulation.sorter;public class Sortdemo {public static void T1 () {Blockjunit 4ClassRunner Runner=null; try {runner= new Blockjunit4classrunner (Unit4.class); Runner.filter (New Methodnamefilter ("Testfilteredout")); } catch (Initializationerror e) {} runner.sort (new Sorter (New Alphabetcomparator ())); Runner.run (New Runnotifier ()); } public static void T2 () {Request request = Request.aclass (Unit4.class); Request = Request.sortwith (new Alphabetcomparator ()); Runner Runner = Request.getrunner (); Runner.run (New Runnotifier ()); } public static void T3 () {Request request = Request.aclass (Unit4.class); Runner Runner = Request.getrunner (); Sorter SorteR=new Sorter (New Alphabetcomparator ()); Sorter.apply (runner); Runner.run (New Runnotifier ()); } public static void Go () {T1 (); T2 (); T3 ();}}
Record it first.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
JUnit4.8.2 Source Analysis-6.1 Sorting and filtering