简体   繁体   中英

how to test Comparator at junit test

I need to test this method - compare() . Can You get advice? How better I can do this(all part if, else-if, else).

public class AbsFigure {

class AreaCompare implements Comparator<FigureGeneral> {

    @Override
    public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
        double firstValue = oneFigure.area();
        double secondValue = twoFigure.area();
        int result = 0;

        if (firstValue > secondValue)
            result = 1;
        else if (firstValue < secondValue)
            result = -1;
        else
            result = 0;

        return result;
    }
}

After this recomendations - we have next picture (Thank YOU guys a lot!):

public AreaCompare areaCompare = new AreaCompare();

@Test
public void testEqual() {
    FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
}

@Test
public void testGreaterThan() {
    FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
}

@Test
public void testLessThan() {
    FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
    FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);

All is normal testing now.

Just instantiate your comparator class and pass in objects:

public class Test extends TestCase {
    class AreaCompare implements Comparator<FigureGeneral> {

        @Override
        public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
            double firstValue = oneFigure.area();
            double secondValue = twoFigure.area();
            int result = 0;

            if (firstValue > secondValue) {
                result = 1;
            } else if (firstValue < secondValue) {
                result = -1;
            } else {
                result = 0;
            }

            return result;
        }
    }

    private final AreaCompare areaCompare = new AreaCompare();

    @Test
    public void testEqual() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
    }

    @Test
    public void testGreaterThan() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
    }

    @Test
    public void testLessThan() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);
    }
}

Looks goof for me. Maybe get rid of result

class AreaCompare implements Comparator<FigureGeneral> {

    @Override
    public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
        double firstValue = oneFigure.area();
        double secondValue = twoFigure.area();
        if (firstValue > secondValue)
            return 1;
        else if (firstValue < secondValue)
            return -1;
        return 0;
    }
}

Write at least test case. One for each return value.

compare(a, b) should have different sign than compare(b, a) or

compare(a, b) == compare(b, a) == 0

I recently had a similar requirement and came up with some helper methods (not published as API yet, though). Here is the source code:

https://github.com/SoftSmithy/softsmithy-lib/blob/master/softsmithy-lib-core/src/test/java/org/softsmithy/lib/Tests.java

Here is a test, which uses these utility methods:

https://github.com/SoftSmithy/softsmithy-lib/blob/master/softsmithy-lib-core/src/test/java/org/softsmithy/lib/util/PositionableComparatorTest.java

It may be better to test for the contract and not for the -1/0/1 values (or more precisely, any positive/zero/negative value). This can be done using Hamcrest matchers in a very concise way. Consider the following example:

import static org.hamcrest.Matchers.comparesEqualTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

public AreaCompare areaCompare = new AreaCompare();

@Test
public void testEqual() {
    FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
    assertThat(oneFigre comparesEqualTo(twoFigure));
    assertThat(twoFigure, comparesEqualTo(oneFigure));
}

@Test
public void testGreaterThan() {
    FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
    assertThat(oneFigure, greaterThan(twoFigure));
}

@Test
public void testLessThan() {
    FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
    FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
    assertThat(oneFigure, lessThan(twoFigure));
}

Thus you do not have to remember which value stands for what and the tests make the intent clear.

After a little bit advice, good test:

public AreaCompare areaCompare = new AreaCompare();

@Test
public void testEqual() {
    FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
}

@Test
public void testGreaterThan() {
    FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
}

@Test
public void testLessThan() {
    FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
    FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM