Fork me on GitHub

Testers for Comparator

Comparator can be tested by using a dedicated support. For instance :

import java.util.Comparator;

import ch.powerunit.Categories;
import ch.powerunit.TestDelegate;
import ch.powerunit.TestSuite;
import ch.powerunit.comparator.ComparatorTester;


public class MyComparatorTest implements TestSuite {

  @TestDelegate
  public final ComparatorTester<Integer, MyComparator> providedSampler = ComparatorTester
      .of(MyComparator.class).withLessSamples(-6, -4)
      .withEqualSamples(12).withGreaterSamples(16, 18).build();
}

The DSL is provided by the class ComparatorTester and start by the call to of specifying the class of the Comparator to be tested. If the Comparator doesn't provide a 0-args public constructor, it is required to provide an instance to be tested, by using, as first step of the DSL, the method usingInstance.

In very specific case, the framework may be able to build sample for the test ; But in real world it is most probably necessary to specify samples for the Comparator. To do so, a sequence of the three methods withLessSamples, withEqualSamples and withGreaterSamples is required to specify samples. When several less or greater samples are provided, they must be provided in the correct order, from the smallest to the biggest.

The previous examples will generate the following test cases :

Having `-6` = `-6` Then result should be 0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-6` < `12` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` > `-6` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-6` < `16` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `16` > `-6` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-6` < `18` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `18` > `-6` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-6` < `-4` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-4` > `-6` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-4` = `-4` Then result should be 0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-4` < `12` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` > `-4` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-4` < `16` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `16` > `-4` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `-4` < `18` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `18` > `-4` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` < `16` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `16` > `12` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` < `18` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `18` > `12` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` = `12` Then result should be 0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `12` = `12` Then result should be 0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `16` < `18` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest
Having `18` > `16` Then result should be >0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest

In the case of error in the test, the result will look like (for one single test cases) :

Having `-6` < `12` Then result should be <0 for the Comparator class class ...MyComparator (with instance ...MyComparator@25d250c6) of ...MyComparatorTest caused by expecting a value less than <0> but <1> was greater than <0>