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

  • Home
  • SEARCH
  • 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 8485507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:40:12+00:00 2026-06-10T20:40:12+00:00

I have set up a JUnit test that is testing a method called copy(File

  • 0

I have set up a JUnit test that is testing a method called copy(File src, File dest) that simply copies the contents of the src file to the dest file. I am using a Scanner to iterate over each file simultaneously (two different Scanners of course), and then comparing each Scanners next() with .equals().

This test fails, telling me the files are not equal. But how can this be? The strings look identical when I print them, not to mention I did a hex dump of the files right after the call to copy() and those look identical as well. However, when I print each value of next() in bytes, I do indeed get different byte patterns. I am confused as to why this is happening, and what changes I can make to my code to account for this?

My thinking is that it has something to do with the encoding of the files, perhaps the encoding method used to create the files is different then the one used by copy() elsewhere in the program? Really not too sure, any help is appreciated! Here is what I am working with for the test unit:

// The @Rule and @Before blocks are used as set up helper methods for @Test.
    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();

    private File f1, f2;

    @Before
    public void createTestData() throws IOException {
        f1 = tmp.newFile("src.txt");
        f2 = tmp.newFile("dest.txt");

        BufferedWriter out = new BufferedWriter(new FileWriter(f1));
        out.write("This should generate some " +
                "test data that will be used in " +
                "the following method.");
        out.close();
    }

    @Test
    public void copyFileTest() throws FileNotFoundException, 
    Exception {
        try {
            copyFile(f1, f2);
        } catch (IOException e) {
            e.getMessage();
            e.printStackTrace();
        }

        Scanner s1 = new Scanner(f1);
        Scanner s2 = new Scanner(f2);

        // FileReader is only used for debugging, to make sure the character
        // encoding is the same for both files.
        FileReader file1 = new FileReader(f1);
        FileReader file2 = new FileReader(f2);
        out.println("file 1 encoding: " +file1.getEncoding());
        out.println("file 2 encoding: " +file2.getEncoding());

        while (s1.hasNext() && s2.hasNext()) {
            String original = s1.next();
            String copy = s2.next();

            // These print out to be the same ...
            out.println("\ns1: " +original);
            out.println("s2: " +copy);

            // Nevertheless, this comparison fails!
            // These calls to getBytes() return different values.
            if (!(s1.equals(s2))) {
                out.println("\nComparison failed!! \ns1 in bytes: " +original.getBytes()+ 
                        "\ns2 in bytes: " +copy.getBytes());
                fail("The files are not equal.");
            }
        }
    }

And here is my output:

file 1 encoding: UTF8
file 2 encoding: UTF8

s1: This
s2: This

Comparison failed!! 
s1 in bytes: [B@16f5b392
s2 in bytes: [B@5ce04204
  • 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-10T20:40:14+00:00Added an answer on June 10, 2026 at 8:40 pm

    Scanner doesn’t override Object.equals(), so it’s comparing references, which in your case are not equal since you you have two separate Scanner objects.

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

Sidebar

Related Questions

I am trying to test a method that copies a source file to a
Have a JUNIT test set up as such @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ /applicationContext.xml, /applicationContext-security.xml }) @TransactionConfiguration(defaultRollback
I have a JUnit 4 test that loops through an array of test data:
I've got a method that I'd like to test with JUnit/Mockito. The method takes
I have a simple test set up in JUnit, along the lines of public
I have list of items like this: <ItemGroup> <ToCompile Include=clojure\core.clj;clojure\set.clj;clojure\zip.clj;clojure\test\junit.clj;/> </ItemGroup> And I want
We have a JUnit test suite that we usually run from Eclipse. We have
I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set of flat files (114 files) each file is named with database
I have set up an app that is registered for remote notifications. - (void)application:(UIApplication*)application

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.