Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:14:03+00:00 2026-06-10T12:14:03+00:00

I need to write a JUnit test which will compare two csv files of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:14:04+00:00Added an answer on June 10, 2026 at 12:14 pm

    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:

    String[] line = currentLine.split(",")
    

    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:

    assertEquals("item "+file1.line[0]+" values do not match",
        Double.parseDouble(file1.line[1]),
        Double.parseDouble(file2.line[1]),
        0.001)
    

    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:

    for(String line: file1.readNextLine()) 
        file1hash.put(line.split(",")[0],line)
    

    Then as you iterate through the second file you can easily do:

    for(String line2: file2.readNextLine())  {
        String line1=file1hash.get(line2.split(",")[0])
    

    to make sure line1 and line2 refer to the same line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a jUnit test for a rather complex application which runs
I need to write a script in Matlab, which will read some data from
Is there any way that i need not write junit test cases and it
I need to write some input data files for a python program, and I
All, I'm attempting to write a Junit test that calls a Web Service client
I need to write some junit tests on Java code that calls Math.random() .
I need to write JUnit tests for an old application that's poorly designed and
Well, we all know test-driven development. I'd need to write a GUI-based library, but
I need write an update statement that used multiple tables to determine which rows
I need to write a program used internally where different users will have different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.