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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:36:45+00:00 2026-06-13T18:36:45+00:00

I am using maven and the standard directory layout. So I have added a

  • 0

I am using maven and the standard directory layout. So I have added a testdata.xml file in the src/test/resources folder, and I also added it as:

.addAsWebInfResource("testdata.xml", "testdata.xml")

in the deployment method, and I have confirmed that it is there. This will make the file appear in /WEB-INF/testdata.xml. Now I need to have a reference to this file in my code and I tried several different getClass().getResourceAsStream(...) and failing again and again so I need some advise now.

I need it for my DBUnit integration test. Is this not possible?

  • 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-13T18:36:46+00:00Added an answer on June 13, 2026 at 6:36 pm

    Option A) Use ServletContext.getResourceXXX()

    You should have a Aquillarian MockHttpSession and a MockServletContext. E.g.:

    @Test
       public void myTest()
       {
          HttpSession session = new MockHttpSession(new MockServletContext());
          ServletLifecycle.beginSession(session);
    
          ..testCode..
    
          // You can obtain a ServletContext (will actually be a MockServletContext 
          // implementation):
          ServletContext sc = session.getServletContext();
          URL url = sc.getResource("/WEB-INF/testdata.xml")
          Path resPath = new Path(url);
          File resFile = new File(url);
          FileReader resRdr = new FileReader(resFile);
          etc...
    
          ..testCode..
    
          ServletLifecycle.endSession(session);
       }
    

    You can create resource files & subdirectories in:

    1. the web module document root – resources are accessible from the browser and from classes
    2. WEB-INF/classes/ – resources are accessible to classes
    3. WEB-INF/lib/*.jar source jar – accessible to classes
    4. WEB-INF/lib/*.jar dedicated resource-only jar – accessible to classes
    5. WEB-INF/ directly within directory – accessible to classes. This is what you are asking for.

    In all cases the resource can be accessed via:

    URL url = sc.getResource("/<path from web doc root>/<resourceFileName>");
    OR    
    InputStream resIS = sc.getResourceAsStream("/<path from web doc root>/<resourceFileName>");
    

    >

    These will be packaged into the WAR file and may be exploded into directories on the deployed app server OR they may stay within the WAR file on the app server. Either way – same behaviour for accessing resources: use ServletContext.getResourceXXX().

    Note that as a general principle, (5) the top-level WEB-INF directory itself is intended for use by the server. It is ‘polite’ not to put your web resources directly in here or create your own directory directly in here. Instead, better to use (2) above.

    JEE5 tutorial web modules
    JEE6 tutorial web modules

    Option B): Use Class.getResourceXXX()

    First move the resource out of WEB-INF folder into WEB-INF/classes (or inside a jar WEB-INF/lib/*.jar).

    If your test class is:

    • com.abc.pkg.test.MyTest in file WEB-INF/classes/com/abc/pkg/test/MyTest.class

    And your resource file is

    • WEB-INF/classes/com/abc/pkg/test/resources/testdata.xml (or equivalent in a jar file)

    Access File using Relative File Location, via the Java ClassLoader – finds Folders/Jars relative to Classpath:

    java.net.URL resFileURL = MyTest.class.getResource("resources/testdata.xml");
    File resFile = new File(fileURL);    
    OR
    InputStream resFileIS = 
         MyTedy.class.getResourceAsStream("resources/testdata.xml");
    

    Access File Using full Package-like Qualification, Using the Java ClassLoader – finds Folders/Jars relative to Classpath:

    java.net.URL resFileURL = MyTest.class.getResource("/com/abc/pkg/test/resources/testdata.xml");
    File resFile = new File(fileURL);        
    OR 
    InputStream resFileIS = 
         MyTest.class.getResourceAsStream("/com/abc/pkg/test/resources/testdata.xml");   
    OR 
    java.net.URL resFileURL = MyTest.class.getClassLoader().getResource("com/abc/pkg/test/resources/testdata.xml");
    File resFile = new File(fileURL);       
    OR 
    InputStream resFileIS = 
         MyTest.class.getClassLoader().getResourceAsStream("com/abc/pkg/test/resources/testdata.xml");
    

    Hope that nails it! @B)

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

Sidebar

Related Questions

For my web application I am using the Maven Standard Directory Layout. With the
I am using maven to build applications. I have deployed a WAR file containing
In an Eclipse project using the standard Maven layout, renaming a package in the
I'm working with a project that is setup using the standard Maven directory structure
I have a Java web-app using standard web.xml and servlets. At the moment, I'm
We're using maven 2.1.0. I have multiple modules that are completely separate, but still
I have a standard Maven webapp structure defined, and it uses Spring MVC. I
I'm using maven / surefire / eclipse to write some code and later test
I'm using gwt with maven plugin. My web.xml looks like this: <web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/javaee
I'm using Maven 3.0.3. I'm running the mvn test command, in which my test

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.