【问题标题】:JUnit Test for Comparator in JavaJava中比较器的JUnit测试
【发布时间】:2014-06-25 12:48:22
【问题描述】:

如何使用 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;
   }     
}

【问题讨论】:

  • 如果您尝试阅读 JUnit 简介,您应该会发现上面的内容足够简单,可以测试 vogella.com/tutorials/JUnit/article.html
  • 提示:如果你的比较器比较两个空对象会发生什么?一个空对象?由于TreeSet 的性质,这些听起来像是可以尝试的好案例。

标签: java unit-testing junit junit4


【解决方案1】:

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

首先要帮助您解决此问题 - 比较器有三个可能的返回值 -> 为每个返回值编写一个案例。

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

【讨论】:

  • 嗨阿米特让我也上传组件类
  • 另请注意Comparator 可能会测试null 参数,您可能也想测试它。
  • 我添加了部分组件类,请看一下
  • Umair 我认为你应该从这里拿走它。浏览教程。
【解决方案2】:

单元测试应该测试所有可能的结果。 比较器具有三个成功结果。 您需要决定如何处理空参数值(您当前的解决方案: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);
    }
}

【讨论】:

  • 测试类名应该是 ComponentComparatortest 而不是 ComponentTest 因为我们正在测试 ComponentComparator 类的行为。我说的对吗?
【解决方案3】:

这样的事情怎么样:

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

【讨论】:

    【解决方案4】:

    对于任何Comparator 实现,您都应该进行大量测试。

    首先,Comparator 应该在给定类型上定义 (as stipulated in the Comparator contract) total order

    这归结为三件事:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多