title: Maven build error on Junit test case for service layer
body: <p>I am trying to write a simple Junit test case to test service/dao layer of my project. But I get compilation error when I build it using Maven. Kindly help me with this.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class) 
  @ContextConfiguration(locations = {"/C:/project/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml"})
  public class AServiceTest { 
  
      @Autowired
      private AService aService;
  
      @Test
      public void testValidate() throws Exception {
  
          String user = "user10";
          String pass = "pass10";
          String flag = "failure";
  
          User response = aService.validate(user, pass);
  
          if (user.equalsIgnoreCase(response.getUser()) &amp;&amp;  pass.equals(response.getPass())) {
              flag="success";
  
              assertEquals("success", flag);
          }
      }
  }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>package org.springframework.test.context does not exist
  </code></pre>
  
  <p><strong>Update:</strong> </p>
  
  <p>The dependency for spring-test was missing my pom.xml, I added the same and the above error is gone but I get a new error now.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3b5587] to prepare 
  
  test instance [com.sample.project.controller.AServiceTest@16c1857]
  java.lang.IllegalStateException: Failed to load ApplicationContext
          at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
          at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
          at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
          at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
          at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
          at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
          at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
          at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
          at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
          at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
          at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
          at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
          at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
          at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
          at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
          at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
          at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:601)
          at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
          at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
          at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
          at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
          at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [C:/project/src/main/webapp/WEB-
  
  INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [C:/project/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml]
          at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
          at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
          at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
          at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
          at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
          at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
          at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
          at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
          at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
          at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
          ... 30 more
  Caused by: java.io.FileNotFoundException: class path resource [C:/project/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml] cannot be opened because it does not exist
          at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
          at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
          ... 39 more
  </code></pre>
  
  <p><strong>Context file:</strong></p>
  
  <p>
  
  <pre><code>xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
  &lt;context:component-scan base-package="com.sample.project" /&gt;
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;jee:jndi-lookup id="sampleDS" jndi-name="java:/comp/env/jdbc/sampleDS" expected-type="javax.sql.DataSource"/&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="sampleDS"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
        &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="myTxManager" /&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt;
      &lt;property name="prefix" value="/WEB-INF/pages/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>