2013-01-03 35 views
5

我需要测试这种方法 - compare()。你能得到建议吗?我能做得更好(所有部分如果,否则 - 如果,否则)。如何测试比较器在junit测试

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

这recomendations后 - 我们有下一张图片(感谢你们很多!):

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

一切都是正常的测试了。

回答

12

只是实例化你的比较级和传递对象:

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

请注意,对于较小的值,比较器不需要精确地返回-1或更小的精确值。任何负数/正数都可以。 – Puce

+0

但您需要重写此方法 - private void assertTrue(String message,int resultValue,int compareValues){} –

+0

您正在初始化oneFigure和twoFigure,就像每次测试时一样。这将导致NullPointerException或至少一个测试失败(取决于FigureGeneral的构造函数)。 – Chris311

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

2
+0

它将重定向到'http:// sourceforge.net/p/softsmithy/_list/hg'。你能否准确地通过你的代码来回答? –

0

一点点的意见后,良好的测试:

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

因此,您不必记住哪个值代表什么,测试使意图清晰。