Can't Import applicationContext in test class

15,345

Solution 1

well, finally i was able to do it as follows (based on spring roo generated application):

i put my applicationContex.xml file inside the directory:

src/main/resources/META-INF/spring

and in the web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

and in the unit test class:

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

Solution 2

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.

Solution 3

I keep my application context files in (src/main/resources) and add the class path suffix to access them from test or web.xml

To access the context file that is located in src/main/resources from (web.xml), add this config to your web.xml

 <context-param>
  <description>
           Context parameters for Spring ContextLoaderListener     
      </description>
   <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:applicationContext.xml
    </param-value>
  </context-param>

  <listener>
     <listener-class>
           org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>

This is how my test config looks like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@Transactional
public class MyTest 
{
}

Solution 4

A different solution is to add the applicationContext to main/src/resources.

Also add (a second one) application context (for the servlet) to META-INF, with only one line that imports the first context from the classpath

  <import resource="classpath:/mypackage/my-servlet.xml"/>

Then you can import the first one into the test, and there are no duplicate definitions.

Share:
15,345
Mahmoud Saleh
Author by

Mahmoud Saleh

I am Mahmoud Saleh an Enthusiastic Software Engineer, Computer Science Graduate, Experienced in developing J2EE applications, Currently developing with Spring,JSF,Primefaces,Hibernate,Filenet. Email: [email protected] Linkedin: https://www.linkedin.com/in/mahmoud-saleh-60465545? Upwork: http://www.upwork.com/o/profiles/users/_~012a6a88e04dd2c1ed/

Updated on July 18, 2022

Comments

  • Mahmoud Saleh
    Mahmoud Saleh almost 2 years

    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.