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 8450369
In Process

The Archive Base Latest Questions

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

I am trying to test a method that copies a source file to a

  • 0

I am trying to test a method that copies a source file to a dest file using JUnit’s TemporaryFolder. I get a Java IOException when I try run this test however. Does it matter where I make the declaration for the folder? (My test class has several different tests in it). And if so, what is the proper way to do it? I ask because I currently have several unit tests above this code, then I try to set up the testing for the file copying. Maybe the @Rule–@Before–@Test block needs to be in its own class? Here is the snippet where I have coded the test:

...other tests...then:

@Rule
public static 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 \n" +
            "test data that will be used in \n" +
            "the following method.");
    out.close();
}

@Test
public void copyFileTest() {

    out.println("file 1 length: " + f1.length());
    try {
        copyFile(f1, f2);
    } catch (IOException e) {
        e.getMessage();
        e.printStackTrace();
    }

    if (f1.length() != f2.length())
        fail();
    else if (!f1.equals(f2))
        fail();
    assertSame(f1, f2);
}

When I run this test class, all 11 of my tests now fail (which previously passed) and I get java.io.IOException: No such file or directory.

  • 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-10T10:57:05+00:00Added an answer on June 10, 2026 at 10:57 am

    So looking at the JUnit Javadoc, I have found out that any declaration under @Rule must be public, and not static. So I took out the static and just have:

    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();
    

    I still do not know for sure if it matters where this declaration is made when you have other unit tests in your class that do not use the @Rule declaration, but this did allow me to run through my tests successfully.

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

Sidebar

Related Questions

I'm trying to unit test a private method that I have attached to my
I am trying to test that a method to load a UI matrix is
I am trying to create a unit test for a method that takes a
My test is trying to assert that a certain dal method was called, with
I'm trying to test a model method using Play Framework 2.0 and Specs2. Global.scala
I'm trying to test a presenter method using a Capybara RSpec matcher. Lets say
I'm trying to test a method that in a particular scenario should throw an
I am trying to create some JUnit tests for a method that requires user
Trying to use NUnit to test a method that adds an object to a
I have a junit test method that takes a CommonsMultipartFile object as a parameter.

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.