wie um zu testen, Komparator bei junit test

Brauche ich um zu testen, diese Methode - compare(). Können Sie Tipps bekommen? Wie besser kann ich dies tun(alle Teil 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;
    }
}

Nachdem diese Empfehlungen - wir haben nächste Bild (Danke EUCH Jungs eine Menge!):

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

Alles ist normal testen.

InformationsquelleAutor nazar_art | 2013-01-03

Schreibe einen Kommentar