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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:34:44+00:00 2026-05-24T23:34:44+00:00

I am using JUnit to do some automated tests on my application, the tests

  • 0

I am using JUnit to do some automated tests on my application, the tests are in a folder called tests in my JUnit classes package.
As of now, I access the files using the following:

File file = new File(MyClass.class.getResource("../path/to/tests/" + name).toURI());

Is there a cleaner (and nicer) way to do it?

Thanks

  • 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-05-24T23:34:44+00:00Added an answer on May 24, 2026 at 11:34 pm

    If you want to use the classloader to load your test data, then you can’t use File. A File instance represents a path in the file system. The class loads files from the file system, or from jar files, or from zip files, or from somewhere else. The class loader thus doesn’t let you access files, but resources.

    Use MyClass.class.getResourceAsStream("/the/absolute/path.txt") to load the contents, as an input stream, of the path.txt file. This file must be anywhere in the classpath: whether path.txt is in the file system or in a jar doesn’t matter as soon as it can be found in the classpath, in the package the.absolute. So if your data files are in a tests folder, which is just under the sources directory of your tests, the path to use should be /tests/data.txt. Note that this works because Eclipse automatically “compiles” files which are not Java files by just copying them to the output directory (bin or classes, traditionally), and that this directory is in the classpath when you run your tests.

    If you want to load this data as text rather than bytes, just wrap the InputStream with an InputStreamReader.

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

Sidebar

Related Questions

Good afternoon, I am running some JUnit tests on my application using ant. In
I'm trying to write some GUI and integration tests using JUnit and FEST. Here
I'm trying to run some tests in Ant presently using JUnit, and all of
I'm developing an Eclipse SWT application using Eclipse. There are also some JUnit 4
I am attempting to run some automated tests in Eclipse using Java (running them
I'm using Maven 3.0.3. I want to run some Junit tests in my test
I'm using Spring 3.1.0.M2. I'm writing some JUnit 4 tests to test some database
I've implemented access to a database using SQLiteOpenHelper from the android.database package within some
i am writing some junit tests, using an EJBContainer. All works fine, except getting
I'm using the JUnit runner in IntelliJ to run all tests in a package.

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.