How to set up web application context in Spring MVC test

We have a clear abstraction between the service level configurations and the layer context, and we load them as shown below.

Root application context: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value> </context-param> Web application context: <servlet> <servlet-name>lovemytasks</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/mmapp-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

Now we are trying to introduce SPRING MVC TEST FRAMEWORK to test our application.

To do this, I will need to configure the same environment as my real web application.

How can i do this?

I tried the below configuration of my test to load both contexts.

 @ContextConfiguration(locations = { "classpath*:META-INF/spring/applicationContext*.xml", "file:src/main/webapp/WEB-INF/spring/mmapp-servlet.xml" }) 

But his mistake is because

 Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Duplicate <global-method-security> detected. 

We defined global security both in the context of the root application and in the context of web applications.

Note. The above problem will not appear when starting my web application. This only happens when I run the MVR SPRING test.

I tried to remove my global security and one place, and then proceed with errors with conversion services when performing my tests. Which warned me that I am not loading the context, as the real SPRING application does.

Now, I would like to configure the SPRING MVC test environment to use or work in the same way that the SPRING web application environment works. Can anyone suggest how I can achieve this?

+6
source share
2 answers

Use the @ContextHierarchy annotation. His javadok describes it well. In your case you will use

 @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext-*.xml" }), @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/mmapp-servlet.xml" }) }) 
+7
source

do not put your appContext in meta-info.

The "normal" way is to have one spring -servlet.xml in your web-inf

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

Andn then imports the various files into an XML file:

 <import resource="classpath:beans.xml"/> 

I am creating a separate instance of appContent for my tests:

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

Your beans should be loaded twice somewhere along the line, do you import beans twice, defining them both in xml and annotating?

+1
source

All Articles