Java中的比较器JUnit测试

时间:2014-06-25 12:48:23

标签: java unit-testing junit junit4

如何使用JUnit测试测试以下类。我是单元测试的新手,我只需要一个推动开始

public class ComponentComparator implements Comparator< Component >
{

@Override
public int compare ( final Component c1, final Component c2 )
{
    if ( c1.getBandwidthWithHeader() > c2.getBandwidthWithHeader() )
    {
        return -1;
    }
    else if ( c1.getBandwidthWithHeader() < c2.getBandwidthWithHeader() )
    {
        return 1;
    }
    return 0;
}
}

组件类的一部分是,没有此类的构造函数

public class Component
{
   private float bandwidthwithHeader;

   public void setBandwidthWithHeader ( float bandwidthwithHeader )
   {
       this.bandwidthwithHeader = bandwidthwithHeader;
   }
   public float getBandwidthWithHeader ()
   {
       return this.bandwidthwithHeader;
   }     
}

4 个答案:

答案 0 :(得分:1)

您应该阅读一些关于JUnit的教程。 Morfic的评论指出了一个很好的教程。

首先要帮助你 - 比较器有三种可能的返回值 - &gt;为每个人写了一个案例。

import org.junit.Assert;
import org.junit.Test;

public class ComponentComparatorTest {

    @Test
    public void testCompare() throws Exception {
        ComponentComparator comparator = new ComponentComparator();
        Assert.assertEquals(comparator.compare(new Component(1), new Component(1)), 0);
        Assert.assertEquals(comparator.compare(new Component(2), new Component(1)), -1);
        Assert.assertEquals(comparator.compare(new Component(1), new Component(2)), 1);
    }
}

我正在使用虚拟课程

public class Component {
    int bandwidth;

    public Component(int bandwidth) {
        this.bandwidth = bandwidth;
    }

    public int getBandwidthWithHeader(){
        return bandwidth;
    }
}

答案 1 :(得分:0)

这样的事情怎么样:

package mypackage;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class ComponentComparatorTestCase {
    @Test
    public void testCompareExpectZero() {
        ComponentComparator sut = new ComponentComparator();

        // create some components to test with
        Component c1 = new Component();
        Component c2 = new Component();

        // execute test
        int result = sut.compare(c1, c2);

        // verify
        assertEquals("Did not get expected result.", result, 0);
    }
}

答案 2 :(得分:0)

单元测试应测试所有可能的结果。 比较器有三个成功结果。 您需要决定如何处理空参数值(您当前的解决方案:NullPointerException)。 这是您当前比较器的单元测试:

public class Component
{
    private int bandwidthWithHeader;

    public int getBandwidthWithHeader()
    {
        return bandwidthWithHeader;
    }

    public void setBandwidthWithHeader(final int newValue)
    {
        bandwidthWithHeader = newValue;
    }
}

public class ComponentTest
{
    private final ComponentComparator componentComparator = new ComponentComparator();

    @Test
    public void negative1()
    {
        Component two = new Component();

        try
        {
            componentComparator.compare(null, two);
            fail("Expected exception was not thrown");
        }
        catch(NullPointerException exception)
        {
            // The NullPointerException is the expected result.
            assertTrue(true);
        }
    }

    @Test
    public void negative2()
    {
        Component one = new Component();

        try
        {
            componentComparator.compare(one, null);
            fail("Expected exception was not thrown");
        }
        catch(NullPointerException exception)
        {
            // The NullPointerException is the expected result.
            assertTrue(true);
        }
    }

    @Test
    public void negative3()
    {
        try
        {
            componentComparator.compare(null, null);
            fail("Expected exception was not thrown");
        }
        catch(NullPointerException exception)
        {
            // The NullPointerException is the expected result.
            assertTrue(true);
        }
    }

    @Test
    public void positive1()
    {
        Component one = new Component();
        Component two = new Component();

        // test one < two
        one.setBandwidthWithHeader(7);
        two.setBandwidthWithHeader(16);

        assertEquals(-1, componentComparator.compare(one, two);

        // test two < one
        one.setBandwidthWithHeader(17);
        two.setBandwidthWithHeader(16);

        assertEquals(1, componentComparator.compare(one, two);

        // test two == one
        one.setBandwidthWithHeader(25);
        two.setBandwidthWithHeader(25);

        assertEquals(0, componentComparator.compare(one, two);
    }
}

答案 3 :(得分:0)

您应该为任何Comparator实施进行大量测试。

首先,比较器应该在给定类型上定义(as stipulated in the Comparator contracttotal order

这归结为三件事:

  • 顺序应该是反对称的:如果a≤b且b≤a则a = b
  • 顺序应该是可传递的:如果a≤b且b≤c则a≤c
  • 订单应为总数:a≤b或b≤a

其次,Comparator实现可以选择接受空值。因此,如果接受,我们需要测试验证是否正确处理了空值。或者,如果他们不被接受,他们正确地导致NullPointerException被抛出。

最后,如果要比较的类型可能是子类,则值得测试比较器是否正确比较了与类本身实例混合的各种子类的实例。 (您可能需要在测试范围中为这些测试定义一些子类)

由于这些测试往往会针对每个Comparator实现重复,因此可能值得在抽象测试超类中提取它们。