简体   繁体   English

如何在junit测试中测试比较器

[英]how to test Comparator at junit test

I need to test this method - compare() .我需要测试这个方法 - compare() Can You get advice?你能得到建议吗? How better I can do this(all part if, else-if, else).我能做得多好(所有部分 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也许摆脱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)

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).我最近有一个类似的需求,并提出了一些辅助方法(虽然尚未作为 API 发布)。 Here is the source code:这是源代码:

https://github.com/SoftSmithy/softsmithy-lib/blob/master/softsmithy-lib-core/src/test/java/org/softsmithy/lib/Tests.java 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 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).最好测试合约而不是 -1/0/1 值(或更准确地说,任何正/零/负值)。 This can be done using Hamcrest matchers in a very concise way.这可以以非常简洁的方式使用 Hamcrest 匹配器来完成。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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