Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Import applicationContext in test class

my applicationContext.xml,webmvc-config.xml are in WEB-INF/spring/applicationContext.xml

when i try the following, it doesn't load, and i get java.io.FileNotFoundException

@ContextConfiguration(locations = { "classpath:WEB-INF/spring/applicationContext.xml" })

i am using spring 3, junit 4.7.

it works with the dirty workaround by copying applicationContext.xml in resources folder, so it's duplicated.

and i changed the loading to:

@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })

my web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <!-- start up and shut down Spring's root WebApplicationContext (Interface to provide configuration for a web application) -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Central dispatcher for HTTP request handlers/controllers: take an incoming URI and find the right combination of handlers (generally methods on Controller classes) 
  and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location. -->
  <servlet>
    <servlet-name>p</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
            /WEB-INF/spring/webmvc-config.xml                       
            </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>p</servlet-name>
    <url-pattern>/p/*</url-pattern>
  </servlet-mapping>

  <!-- allows one to specify a character encoding for requests. 
  This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form -->
  <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring/applicationContext.xml
    </param-value>
  </context-param>

  <!--  
  <filter>
  <filter-name>springSecurityFilterChain</filter-name> 
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
  </filter>

 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name> 
  <url-pattern>/*</url-pattern> 
  </filter-mapping>  
  -->


  <!-- Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE
       compliant web application server (such as Resin or Tomcat), which allows you to rewrite URLs before they get to your
       code. It is a very powerful tool just like Apache's mod_rewrite. -->
  <filter>
  <filter-name>UrlRewriteFilter</filter-name> 
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>

  <filter-mapping>
  <filter-name>UrlRewriteFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
  </filter-mapping>


</web-app>

please advise a better solution.

like image 938
Mahmoud Saleh Avatar asked Apr 27 '26 10:04

Mahmoud Saleh


1 Answers

The classpath of a web application is composed of

  • WEB-INF/classes
  • all the jars under WEB-INF/lib

So, if you want to load the context file from the classpath, you need it in one of those places. Put it in WEB-INF/classes/spring and load it using classpath:spring/applicationContext.xml for example.

EDIT: I just realized that you have a problem loading the context file from a JUnit test. The answer is similar, though. The directory containing WEB-INF certainly is not in the classpath of your unit test runner. The unit test runner should use more or less the same classpath as the application server, so the file should be in a location that makes it go to a JAR file or a directory that is in the classpath of the test after the build. If using Maven, the src/main/resources directory is normally one of them: everything in that directory goes to the target/classes directory, which is in the classpath of the unit test runner.

like image 90
JB Nizet Avatar answered Apr 29 '26 06:04

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!