Cloud Stack Ninja

I am writing unit tests that check if my method is working properly.

The method is a sorting method that is overriding the compare method from the class Comparator and is sorting a list using Collections.sort(), so I should write tests to see if my sorting method is working properly.

The list contains objects of type Metro where the attributes are : The id,the longitude,the latitude, the name of the station, the district and the mean of transport.

Here is the class Metro.java

public class Metro implements Comparator<Metro>  {

    public int id;
    public double longitude;
    public double latitude;
    public String name; 
    public String district; 
    public String mean; 
    
    public MetroStop() {};
    
    public Metro(int id, double longitude, double latitude, String name, String district, String mean) {
        this.id = id;
        this.longitude = longitude;
        this.latitude = latitude;
        this.name = name;
        this.district = district;
        this.mean = mean;
    }
    
        @Override
        public int compare(Metro m1, Metro m2) {
           Integer metroId1 = m1.id;
           Integer metroId2 = m2.id;
           return metroId1.compareTo(metroId2);
        }
}

In another class : MetroList.java, I create a list of Metro objects and I also implement the method that sorts the objects by the id :

private final Metro comparatorM = new Metro();

public void sortId() {
        Collections.sort(this.list, comparatorM);
    }
    

Here are the unit tests that I have written in order to see if my sorting (by id) is working properly:

public class TestMetro {
public final Metro comparatorM = new Metro();

@Test
public void greaterTest() {
    Metro m1 = new Metro(2, 12.5, 14.3, "Cindy", "London", "Bus");
    Metro m2 = new Metro(1, 14.5, 15.3, "Sara", "Paris", "Tram");
    int c = comparatorM.compare(m1, m2);
    assertTrue(c > 0);
}

@Test
public void lowerTest() {
    Metro m1 = new Metro(1, 12.5, 14.3, "Bob", "Tirana", "Bus");
    Metro m2 = new Metro(2, 14.5, 15.3, "Alice", "Berlin", "Tram");
    int c = comparatorM.compare(m1, m2);
    assertTrue(c < 0);
}

@Test
public void equalTest() {
    Metro m1 = new Metro(2, 12.5, 14.3, "Cindy", "Amsterdam", "Bus");
    Metro m2 = new Metro(2, 14.5, 15.3, "Bob", "Bordeaux", "Tram");
    int c = comparatorM.compare(m1, m2);
    assertTrue(c == 0);
}

@Test
public void triTest() {
    MetroList pa = new MetroList();
    Metro metro1 = new Metro(3, 13.5, 18.7, "Amelie", "Paris", "Tram");
    Metro metro2 = new Metro(7, 12.4, 19.6, "Anna", "Bordeaux", "Bus");
    Metro metro3 = new Metro(2, 11.7, 20.5, "Ben", "Lyon", "Bus");
    Metro metro4 = new Metro(23, 10.5, 27.8, "Cindy", "Durres", "Tram");
    Metro metro5 = new Metro(1, 14.5, 30.2, "Omeria", "Lushnje", "Bus");
                                
    pa.list.add(metro1);
    pa.list.add(metro2);
    pa.list.add(metro3);
    pa.list.add(metro4);
    pa.list.add(metro5);

    pa.sortId();
    ArrayList<Metro> expectedList = new ArrayList<Metro>();
    expectedList.add(metro5);
    expectedList.add(metro3);
    expectedList.add(metro1);
    expectedList.add(metro2);
    expectedList.add(metro4);
    
    assertEquals(pa.list,expectedList);
}

}

Question 1: Are the above tests sufficent in order to check the behaviour of the sorting function? Can you suggest me any other test that can be necessary for the program's stability? I am out of ideas and I can't think of other tests for the sorting by id function.

Question 2: I want to sort also by the district (which is a String), but I can't write two compare methods that will be equal. Can you suggest me a solution?



Read more here: https://stackoverflow.com/questions/64417827/unit-test-fort-a-list-sorting-in-java

Content Attribution

This content was originally published by User123 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: