Announcement Announcement Module
Collapse
No announcement yet.
How to access app-context XML in Tomcat from my SpringJunit4ClassRunner in Eclipse Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access app-context XML in Tomcat from my SpringJunit4ClassRunner in Eclipse

    Hi folks,

    I encountered a problem to access Spring context (which is running in my Tomcat) from my JUnit (which is running in my workspace).

    Demo of my original code:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
        "classpath:/WEB-INF/applicationContext-resources.xml"
    })
    public class JUnitAccessSpringContextTest
    {
    }
    Unfortunately, above code is NOT working in my case, because values of my "applicationContext-resources.xml" in my workspace will be replaced before deploying to Tomcat.

    To resolve above problem, an alternative way is to let @ContextConfiguration points to the file (which locates in Tomcat/webapps) directly. It works in my system (shown by the following code), but it is NOT good for real use because the "path_to_my_tomcat_application" is hard coded.

    Change of my code:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
        "file:<path_to_my_tomcat_application>/applicationContext-resources.xml"
    })
    public class JUnitAccessSpringContextTest
    {
    }
    Question:

    1) Can I insert some kind of the variable into the @ContextConfiguration "file" String, such as some read String from a property file, or system values (e.g., System.getenv("CATALINA_HOME"))?

    ---

    One more alternative way is to set TOMCAT_HOME into the classpath of my Eclipse, and write the relative path into the @ContextConfiguration.

    For example, if I set the TOMCAT_HOME/project_name into my Eclipse's classpath, the following code works fine:

    Code:
    @ContextConfiguration(locations = {
        "classpath:WEB-INF/applicationContext-resources.xml"
    })
    However, because everytime the deployed "project_name" is different from each other, so I cannot put it into my Eclipse path, --> I can only put the entire tomcat home (TOMCAT_HOME) into my Eclipse classpath. But in this case, the following code does NOT work. why?

    Code:
    @ContextConfiguration(locations = {
        "classpath:*/WEB-INF/applicationContext-resources.xml"
    })
    Question:

    2) why above code is NOT working?

    Additional information:

    To make the things even more complicated. We are running on a old Spring version (2.5) for the project. But I think if necessary, I can change the version spring-test.jar at least -- it should not affect the running project. Am I right?


    Thanks a lot!!

    Ye
Working...
X