title: Spring Maven JUnit NoClassDefFoundError:org/springframework/context /ApplicationContextException
body: <p>I have created a spring legacy project using Spring STS and xml configuration file <code>spring-context.xml</code>. I am trying to test my DAO class using JUnit (<strong>Run as > Maven test</strong>) but I get the following error:</p>
  
  <pre><code>initializationError(net.mozaikdev.samples.dao.test.AddressDAOTest)  Time elapsed: 0.006 sec  &lt;&lt;&lt; ERROR!
  org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.test.context.support.DelegatingSmartContextLoader]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextException
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:177)
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:123)
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:146)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.resolveContextLoader(AbstractTestContextBootstrapper.java:467)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:357)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:303)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:109)
  at org.springframework.test.context.TestContextManager.&lt;init&gt;(TestContextManager.java:135)
  at org.springframework.test.context.TestContextManager.&lt;init&gt;(TestContextManager.java:120)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.&lt;init&gt;(SpringJUnit4ClassRunner.java:142)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
  at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
  at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
  at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
  at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
  at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
  at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
  at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
  at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
  at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
  at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
  Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextException
  at org.springframework.test.context.support.DelegatingSmartContextLoader.&lt;init&gt;(DelegatingSmartContextLoader.java:63)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:165)
  ... 32 more
  Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextException
  at java.net.URLClassLoader.findClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  ... 38 more
  </code></pre>
  
  <p>I don't understand why I'm getting this exception since I have declared in my pom.xml the dependency to spring-context:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;net.mozaikdev&lt;/groupId&gt;
  &lt;artifactId&gt;eshop-sample-spring-legacy&lt;/artifactId&gt;
  &lt;name&gt;eshop-sample-spring-legacy&lt;/name&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;version&gt;1.0.0-BUILD-SNAPSHOT&lt;/version&gt;
  
  &lt;repositories&gt;
      &lt;repository&gt; 
          &lt;id&gt;repository.spring.release&lt;/id&gt; 
          &lt;name&gt;Spring GA Repository&lt;/name&gt; 
          &lt;url&gt;http://repo.spring.io/release&lt;/url&gt; 
      &lt;/repository&gt;
  
      &lt;repository&gt; 
          &lt;id&gt;repository.spring.milestone&lt;/id&gt; 
          &lt;name&gt;Spring Milestone Repository&lt;/name&gt; 
          &lt;url&gt;http://repo.spring.io/milestone&lt;/url&gt; 
      &lt;/repository&gt;
  &lt;/repositories&gt;
  
  &lt;properties&gt;
      &lt;java-version&gt;1.8&lt;/java-version&gt;
      &lt;org.springframework-version&gt;5.0.0.RC3&lt;/org.springframework-version&gt;
      &lt;org.hibernate-version&gt;5.2.10.Final&lt;/org.hibernate-version&gt;
      &lt;org.aspectj-version&gt;1.8.10&lt;/org.aspectj-version&gt;
      &lt;org.slf4j-version&gt;1.7.25&lt;/org.slf4j-version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
  
      &lt;!-- Spring --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                  &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
               &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;${org.hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- MariaDB Connector J --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.mariadb.jdbc&lt;/groupId&gt;
          &lt;artifactId&gt;mariadb-java-client&lt;/artifactId&gt;
          &lt;version&gt;2.1.1&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- HikariCP connection pool --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
          &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
          &lt;version&gt;2.7.0&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Here is my JUnit test:</p>
  
  <pre><code>@ContextConfiguration(locations = "classpath:application-context.xml")
  @RunWith(SpringJUnit4ClassRunner.class)
  public class AddressDAOTest {
  
  @Autowired
  private AddressDAO addressDAO;
  
  private static final String STREET_1 = "street_1";
  private static final String STREET_2 = "street_2";
  private static final Integer ZIP_CODE = new Integer(1234);
  private static final String CITY = "city";
  private static final String REGION = "region";
  private static final String COUNTRY = "country";
  private static final Boolean IS_DELIVERY_ADDRESS = new Boolean(false);
  
  @Test
  @Transactional
  @Rollback(value = true)
  public void testPersistAndFindById() {
  
      Address address = new Address();
      address.setStreet_1(STREET_1);
      address.setStreet_2(STREET_2);
      address.setZipCode(ZIP_CODE);
      address.setCity(CITY);
      address.setRegion(REGION);
      address.setCountry(COUNTRY);
      address.setIsDeliveryAddress(IS_DELIVERY_ADDRESS);
  
      addressDAO.persist(address);
  
      Long id = address.getId();
  
      address = addressDAO.findById(id);
  
      Assert.assertEquals(STREET_1, address.getStreet_1());
      Assert.assertEquals(STREET_2, address.getStreet_2());
      Assert.assertEquals(ZIP_CODE, address.getZipCode());
      Assert.assertEquals(CITY, address.getCity());
      Assert.assertEquals(REGION, address.getRegion());
      Assert.assertEquals(COUNTRY, address.getCountry());
      Assert.assertEquals(IS_DELIVERY_ADDRESS, address.getIsDeliveryAddress());
  
  }
  
  @Test
  @Transactional
  @Rollback(value = true)
  public void testFindAll() {
  
      Address address1 = new Address();
      address1.setStreet_1(STREET_1);
      address1.setStreet_2(STREET_2);
      address1.setZipCode(ZIP_CODE);
      address1.setCity(CITY);
      address1.setRegion(REGION);
      address1.setCountry(COUNTRY);
      address1.setIsDeliveryAddress(IS_DELIVERY_ADDRESS);
  
      Address address2 = new Address();
      address2.setStreet_1(STREET_1_UPDATE);
      address2.setStreet_2(STREET_2_UPDATE);
      address2.setZipCode(ZIP_CODE_UPDATE);
      address2.setCity(CITY_UPDATE);
      address2.setRegion(REGION_UPDATE);
      address2.setCountry(COUNTRY_UPDATE);
      address2.setIsDeliveryAddress(IS_DELIVERY_ADDRESS_UPDATE);
  
      addressDAO.persist(address1);
      addressDAO.persist(address2);
  
      List&lt;Address&gt; addressesList = addressDAO.findAll();
  
      Assert.assertNotNull(addressesList);
      Assert.assertTrue(addressesList.size() &gt;= 2);
      Assert.assertTrue(addressesList.contains(address1));
      Assert.assertTrue(addressesList.contains(address2));
  }
  }
  </code></pre>
  
  <p>And finally the <code>application-context.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
  &lt;!-- Enable annotation configuration --&gt;
  &lt;context:annotation-config /&gt;
  
  &lt;!-- Scan packages for Spring annotations --&gt;
  &lt;context:component-scan base-package="net.mozaikdev.samples.dao" /&gt;
  &lt;context:component-scan base-package="net.mozaikdev.samples.dao.test" /&gt;
  
  &lt;!-- Database connection configuration --&gt;
  &lt;bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"&gt;
      &lt;property name="jdbcUrl" value="${spring.datasource.jdbcUrl}"/&gt;
      &lt;property name="driverClassName" value="${spring.datasource.driverClassName}"/&gt;
      &lt;property name="username" value="${spring.datasource.username}"/&gt;
      &lt;property name="password" value="${spring.datasource.password}"/&gt;
      &lt;property name="poolName" value="${spring.datasource.hikari.poolName}"/&gt;
      &lt;property name="connectionTimeout" value="${spring.datasource.hikari.connectionTimeout}"/&gt;
      &lt;property name="maximumPoolSize" value="${spring.datasource.hikari.maximumPoolSize}"/&gt;
      &lt;property name="idleTimeout" value="${spring.datasource.hikari.idleTimeout}"/&gt;
      &lt;property name="connectionTestQuery" value="${spring.datasource.hikari.connectionTestQuery}"/&gt;
      &lt;property name="maxLifetime" value="${spring.datasource.hikari.maxLifetime}"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- HikariCP datasource bean --&gt;
  &lt;bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"&gt;
      &lt;constructor-arg index="0" ref="hikariConfig"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Enables annotation-driven transaction management. --&gt;
  &lt;tx:annotation-driven/&gt;
  
  &lt;!--
      Creates the transaction manager bean that integrates the used JPA provider with the
      Spring transaction mechanism.
  --&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Specifies the default settings that are compatible with Hibernate. --&gt;
  &lt;bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
  
  &lt;!-- Creates the Hibernate entity manager factory bean --&gt;
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/&gt;
      &lt;property name="packagesToScan" value="net.mozaikdev.samples.domain"/&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${spring.jpa.properties.hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${spring.jpa.properties.hibernate.show_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;${spring.jpa.properties.hibernate.format_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.generate_statistics"&gt;${spring.jpa.properties.hibernate.generate_statistics}&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Enable support for annotation-driven Spring MVC controllers --&gt;
  &lt;!-- &lt;mvc:annotation-driven/&gt; --&gt;
  </code></pre>
  
  <p></p>
  
  <p>I have cleaned my project using <strong>Run as > Maven clean</strong> and <strong>Project > Clean...</strong>, and I updated the project <strong>Maven > Update project</strong> but nothing works. I am stuck now, if somebody can help me to solve this exception it would be great?</p>
  
  <p>Thanks in advance.</p>