简体   繁体   中英

JUnit Test for Comparator in Java

How can I test the following class using JUnit testing. I am new to unit testing I just need a push to start

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

Part of the component class is, there is no constructor for this class

public class Component
{
   private float bandwidthwithHeader;

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

You should go through some tutorial on JUnit. Morfic's comment points to a good tutorial.

To begin with helping you with this - there are three possible return values from the comparator -> wrote a case for each one.

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

I am using a dummy class

public class Component {
    int bandwidth;

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

    public int getBandwidthWithHeader(){
        return bandwidth;
    }
}

How about something like this:

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

The unit test should test all possible outcomes. A comparator has three success outcomes. You need to decide how you want to handle null parameter values (your current solution: NullPointerException). Here is a unit test of your current comparator:

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

There are numerous tests you should do for any Comparator implementation.

First off, there is the fact that a Comparator should define ( as stipulated in the Comparator contract ) a total order on the given type.

This comes down to 3 things :

  • the order should be antisymmetric : if a ≤ b and b ≤ a then a = b
  • the order should be transitive : if a ≤ b and b ≤ c then a ≤ c
  • the order should be total : either a ≤ b or b ≤ a

Secondly, a Comparator implementation may choose to accept null values . So we need tests that verify whether null values are treated correctly, if accepted. Or if they are not accepted that they properly result in a NullPointerException being thrown.

Lastly, if the type being compared may be subclassed , it is worth testing that the comparator properly compares instances of various subclasses mixed with instances of the class itself. (You may need to define some subclasses in test scope for these tests)

As these tests tend to repeat for every Comparator implementation, it may be worth extracting them in an abstract test superclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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