Announcement Announcement Module
Collapse
No announcement yet.
How to set MessageSourceAccessor in SimpleFormController for Unit Test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set MessageSourceAccessor in SimpleFormController for Unit Test

    Hello All,
    I am facing an issue with unit testing a form controller that extends SimpleFormController. When I run the Unit Test, I get the following error.

    Code:
    ApplicationObjectSupport instance [com.abc.MyController] does not run in an Application Context
    java.lang.IllegalStateException
    at org.springframework.context.support.ApplicationObjectSupport.getMessageSourceAccessor(ApplicationObjectSupport.java:137)
    We have mock object for ServletContext. Do we have any mock object for MessageSourceAccessor or do I have to follow a different approach ?

    Waiting for a favorable reply.Thanks in advance
    Kind regards.

    MReeds.

  • #2
    Hello All,
    I've figured out how to set the MessageSourceAccessor.

    Code:
    StaticApplicationContext staticApplicationContext = new StaticApplicationContext();
    Map properties = new HashMap();
    properties.put("basename", "<name of your properties file>");        
    staticApplicationContext.registerSingleton("messageSource",ResourceBundleMessageSource.class,new MutablePropertyValues(properties));
    staticApplicationContext.refresh();
    and in your controller instance.

    Code:
    instance.setApplicationContext(staticApplicationContext);
    Kind Regards.
    MReeds.

    Comment

    Working...
    X