LaVOZs

The World’s Largest Online Community for Developers

'; java - Junit assertion - LavOzs.Com

**I am doing comparision between the two list that is between the csv and db data but I am returning void so my assert is not working.What should be the change that assert will work. I have tried by putting the return below match found and changing the return type to boolean but it does not work.

        public static boolean compareTwoLists(List<Map<String, String>> csvData, List<Map<String, String>> sqlData) {

// Comparator on basis of product Code
        Comparator<Map<String, String>> comp = (o1, o2) -> {
            return o1.get("productcode").compareTo(o2.get("productcode"));
        };
         System.out.println("Test");
        Collections.sort(csvData, comp);

        Collections.sort(sqlData, comp);
        System.out.println("testdef");

        for (Map<String, String> i : sqlData) {
            if (csvData.contains(i)) {
                System.out.println("Match Found " + i);
                return true;
            }else{
                System.out.println(" No Match Found ");
            }
        }

        return false;
    }



        public static List<Map<String, String>> convertToMapFromCsv(String filePath)
                throws FileNotFoundException, IOException {

            BufferedReader in = new BufferedReader(new FileReader(filePath));
            List<Map<String, String>> namefreq = in.lines().skip(1).map(line -> line.split(COMMA)) // splitting csv
                    .map(line -> {
                        Map<String, String> map = new HashMap<>();
                        map.put("productcode", line[0]);
                        map.put("store", line[1]);
                        map.put("itemavailabilityscore", line[2]);
                        map.put("salesunitscore", line[3]);
                        map.put("totalscore", line[4]);
                        map.put("pdp_views", line[5]);
                        return map;
                    }).collect(Collectors.toList());

            System.out.println("testabc");
            return namefreq;
        }
    }
    **//junit assertion**
      In this new generic is of type  List<Map<String,String>> newGeneric which return the db data.FileComparatorUtility is class and the method compareTwoLists is declared above

assertFalse(!FileComparatorUtility.compareTwoLists(csvData,newGeneric));

    Its not working  when I put the following assertion as a junit what should I change in the code that when I do assertion in junit such that when there is match between csv and db data then it return true else it returns false

Are you at least get a correct output (I mean if it displays "Match Found" when it's supposed to be)?
Does your list of Map is correctly parsed from input file?
If both are true, then the issue comes from a different place.

In question comments, you are saying that if there is a single record mismatch between the lists, the assertion should fail.
The assertFalse(boolean) fails when the computation of the given boolean expression returns false.
But yet, there's a ! (boolean negation) before compareTwoLists call, so the compareTwoLists method must to return a false and this returned false will be inverted to true becouse of the ! operator. So, when the whole expression is evaluated to true, the assertion will fail.

Having that, the compareTwoLists method should return false when there is "No Match Found" and true if every element matches (after the loop).

compareTwoLists -> return false
!compareTwoLists -> return true
assertionFalse(!compareTwoLists) -> assertion fails

The "No match Found" indicates the mismatch between both lists and if it returns false, the assertion will also fail.

Some side words:

There is no info in your question about how exactly you need to compare those lists. Does all elements from csv list needs to be found in sql list, the other way (all sql in csv) or both lists should be equal to each other?

When looping through bigger list, it's certain that the smaller list won't contain of given element (at least when there are not duplicates).
For the complete equality, you can use just a listA.equals(listb) over sorted lists.
Depending on which list must contain elements of another, you need to adequatly use it in for loop and if contains check.

I'm assuming that all of the csv elements must be found in sql list. Looping through sql data might be enough, but what if one list has more elements than another?
If you want to check that all csv data is in sql, then you should loop through the csv data. The csv might have more elements than sql and those excessive list elements might not be found in the sql data. But if the csv data can have another elements when others from csv data were found in the sql data the looping throug the sql data is fine.
If there is more data in the sql data than in the csv data, then the csv definitely won't contain some of the elements of the sql (when those elements aren't duplicates).
So here, it should be looping through the csv data and check if its elements are contained in sql data.

Anyway, if you want to check that every element of csv data can be found in sql data, then you should loop through csv data. That will test, if the sql data contains of every element from the csv data.

With the code sample you've provided and my words from the above, the last part of the compareTwoLists could be changed to:

for (Map<String, String> i : csvData) {
    if (!sqlData.contains(i)) {
        System.out.println(" No Match Found: " + i);
        return false;
    }
}
return true;

As you already saw in your code, the word "compare" is kind of reserved for checking if one object is smaller/greater/equal to another (so it returns the int to indicate those states). I'd recommend you to modify that method name to say which list from parameters should be contained in another?
I'd recommend you to modify that method name to explicitly describe desired output, for example anyContainAnother or isFirstListContainedInSecond (but this might seem to long, IDK if coding standards all of something like does2ndContain1st or is1stIn2nd).
Trying to describe a method behaviour by name simplifies understanding of the code, which later could simplify the code iself. Like to remove the ! from assertion or to use the assertTrue when the desired list comparation returns true.

Additionally, if you're using at least Java8, you could replace the Collection.sort(list,comp) to:

list.sort(comp)

If you are sure that every map element in both lists, has "productcode" then your comparator function is fine.
But if even one map entry won't contain the "productcode" key, then the NullPointerException will be thrown from the sort method, because one of the parameters might be null.
The right implementation for you depends on the other cases I mentioned, but for most you'd need to check if both parameters in comparator function have some (non null) value under the "productcode" key, for example:

Comparator<Map<String, String>> comp = (o1, o2) -> {
    if (o1.get("productcode") == null) {
        return -1;
    }
    if (o2.get("productcode") == null) {
        return 1;
    }
    return o1.get("productcode").compareTo(o2.get("productcode"));
};

Sorry for the LOOOOONG answer, probably you didn't need most of those explanations and IDK if they seems to be useful for others, but lets leave it here. :)

Related
How do you assert that a certain exception is thrown in JUnit 4 tests?
differences between 2 JUnit Assert classes
Why doesn't JUnit provide assertNotEquals methods?
JUnit test for System.out.println()
Conditionally ignoring tests in JUnit 4
How to do a JUnit assert on a message in a logger
Assert equals between 2 Lists in Junit
Maven does not find JUnit tests to run
How to assert greater than using JUnit Assert?
JUnit 5: How to assert an exception is thrown?