I need to write a JUnit test which will compare two csv files of same format and will pass only if their absolute difference is less than threshold.
I need exact match for strings and for double,it should satisfy threshold criteria.
CSV FORMAT:
first.csv Name price-1 price-2 item1 5.12 6.12 item2 4.23 5.56 item3 11.2 12.23 second.csv Name price-1 price-2 item1 5.12 6.10 item2 4.20 5.50 item3 11.19 12.19
Now lets say difference threshold is 0.15.
so here absolute difference between price1 of item2 in first.csv and second.csv is 0.03
then it will pass JUnit test and if difference threshold is 0.02 then it will fail.
what can be good solution for it?
You listed junit in the tag.
Junit’s .equals(double, double, accuracy) allows you to specify how close they have to be with the last parameter.
I’d just read in the values and call .equals for each in a test…
or is there something to the question I’m not getting?
To parse the lines, your examples use spaces but you say “CSV” (Comma Separated). If they actually are CSV you could use something like:
on each line. That would give you line[0]=”item1″, line[1]=”5.12″, line[2]=”6.12″
After that try parsing line[1] and line[2] with Double.parseDouble()
By the way, use assertEquals, not assertTrue, the more specific assertEquals will display the value you wanted and the value you got as part of your error in the junit results.
I also recommend you pass in the optional string. The test line would look like this:
There is also the whole problem of making sure you are reading the same line for each file–getting them paired right. If they are guaranteed to be in the same order you are fine, but if not you might want to hash up the first file by the name field:
Then as you iterate through the second file you can easily do:
to make sure line1 and line2 refer to the same line.