簡體   English   中英

如何在junit測試中測試比較器

[英]how to test Comparator at junit test

我需要測試這個方法 - compare() 你能得到建議嗎? 我能做得多好(所有部分 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;
    }
}

在此推薦之后 - 我們有下一張照片(非常感謝你們!):

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);

現在一切正常測試。

只需實例化您的比較器類並傳入對象:

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);
    }
}

對我來說看起來很傻。 也許擺脫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;
    }
}

至少編寫測試用例。 每個返回值一個。

compare(a, b)符號應與compare(b, a)

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

最好測試合約而不是 -1/0/1 值(或更准確地說,任何正/零/負值)。 這可以以非常簡潔的方式使用 Hamcrest 匹配器來完成。 考慮以下示例:

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));
}

因此,您不必記住哪個值代表什么並且測試使意圖清晰。

經過一點建議,良好的測試:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM