Java Spring MVC + JUNIT + Not able to create a beans

By : Johnny

I am struggling with my spring MVC web app. The app itself looks to work correctly. 2 days back i wanted to make some JUnit tests for my classes and i've read sth how to make it with Spring.

So i created my Test Class and i used below annotation to run tests with Spring and to load a proper contexts.

@ActiveProfiles(profiles = "test")
@ContextConfiguration(locations = {"classpath:/dispatcher-servlet.xml","classpath:/beans.xml","classpath:/jpaContext.xml"}

and when i try to run this test through maven or just by eclipse, im getting following error:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource' defined in class path resource [dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

Can anyone tell me why it happens ? App works correctly without testing and all beans are loaded without any issue.

WHy i have such a problem with tests?

Thanks in advance ! Jan

By : Johnny


You should add servlet-api as a dependency.


Check this answer for more information.

You can implement parent class Machine with common logic and children classes with personal logic.

rails g model machine type:string customer_id:integer
mkdir app/models/machines
touch app/models/machines/bmw.rb
touch app/models/machines/renault.rb


class Machine < ActiveRecord::Base
  belongs_to :customer
  def country
    raise NotImplementedError


class Bmw < Machine
  def country


class Renault < Machine
  def country

For example:

  • First bmw Bmw.first
  • First any car Machine.first
  • Get customer first Renault Renault.first.customer
  • Country any car, this method will call from any the child class.

In vanilla javascript:


With jquery use .attr():


This video can help you solving your question :)
By: admin