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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:06:17+00:00 2026-06-04T04:06:17+00:00

I have a web application, that contains a configuration xml file for one of

  • 0

I have a web application, that contains a configuration xml file for one of my application services that is exposed as spring bean.
Also I have a standalone java application(which references my web app project from its pom.xml) in the same workspace, that runs tests using Spring TestContext framework and one of the tests checks the configuration of that XML file.

However I have a problem with accessing this xml file from the standalone app:

Before setting-up the test, in my previous configuration, the file was accessed through ServletContext and was located in WEB-INF/ folder. However, to make it accessable from the test project I had to move it to source/ folder and load it with getClassLoader().getResourceAsStream() method instead that of ServletContext. But it makes editing the file cumbersome because every time the app has to be redeployed.

Is it possible to keep the file in WEB-INF/ folder but load it from the referencing project during the test-runs?

P.S. Currently it’s an STS project with Tomcat server.

  • 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-04T04:06:18+00:00Added an answer on June 4, 2026 at 4:06 am

    I ended up using Spring MockServletContext class and injecting it directly to my service bean, before the test runs, as my service implemented ServletContextAware :

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/test-ctx.xml" } ) 
    public class SomeServiceTest {
    
    @Autowired
    private MyServletContextAwareService myService;
    
    @Before
    public void before(){
                //notice that I had to use relative path because the file is not available in the test project
        MockServletContext mockServletContext = new MockServletContext("file:../<my web project name>/src/main/webapp"); 
    
        myService.setServletContext(mockServletContext);        
    }
    

    If I had several classes using Servlet Context, then the better solution would be to use WebApplicationContext instead the default one (currently provided by DelegatingSmartContextLoader), but it would require implementing custom ContextLoader class and passing its class name to @ContextConfiguration annotation.

    alternative and somewhat cleaner solution which later came to my mind is to refactor the service and inject ServletContext via @Autowired instead of messing with ServletContextAware, and provide the bean of corresponding type(effectively a MockServletContext instance).

    Possibly, in future the direct support of MockServletContext from test classes will be added to Spring see SPR-5399 and SPR-5243.

    UPDATE FOR SPRING 3.2
    In Spring 3.2 initialization of the servlet context became as simple as adding one @WebAppConfiguration annotation:

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration("file:../<my web project name>/src/main/webapp")
    @ContextConfiguration(locations = { "/test-ctx.xml" } ) 
    public class SomeServiceTest {
    

    see details in the article

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

Sidebar

Related Questions

I have a web application with a custom configuration section. That section contains information
I have a web application that's based on Spring and the application contexts, applicationContext.xml
I have a web application that contains a bunch of classes in the App_Code
I have a web application where the masterPage/template contains some static HTML that never
I have this senario. We have an application server that contains a few web
I have a mvc web site that contains one non-mvc page (it is using
I have a solution that contains two projects. One project is an ASP.NET Web
I have an application.war folder (not as file) which contains jaxen-1.1.1.jar (in the WEB-INF/lib)
I have a web application (.war) that contains some static files (e.g. MS word
I have a config.inc file in a web application that I am building. It

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.