Class ApplicationContextMock

  • All Implemented Interfaces:
    java.io.Serializable, org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.beans.factory.ListableBeanFactory, org.springframework.context.ApplicationContext, org.springframework.context.ApplicationEventPublisher, org.springframework.context.MessageSource, org.springframework.core.env.EnvironmentCapable, org.springframework.core.io.ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver

    public class ApplicationContextMock
    extends java.lang.Object
    implements org.springframework.context.ApplicationContext, java.io.Serializable
    Mock application context object. This mock context allows easy creation of unit tests by allowing the user to put bean instances into the context. Only getBean(String), getBean(String, Class), and getBeansOfType(Class) are implemented so far. Any other method throws UnsupportedOperationException.
    Author:
    Igor Vaynberg (ivaynberg)
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from interface org.springframework.beans.factory.BeanFactory

        FACTORY_BEAN_PREFIX
      • Fields inherited from interface org.springframework.core.io.ResourceLoader

        CLASSPATH_URL_PREFIX
      • Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver

        CLASSPATH_ALL_URL_PREFIX
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsBean​(java.lang.String name)  
      boolean containsBeanDefinition​(java.lang.String beanName)  
      boolean containsLocalBean​(java.lang.String arg0)  
      <A extends java.lang.annotation.Annotation>
      A
      findAnnotationOnBean​(java.lang.String beanName, java.lang.Class<A> annotationType)  
      java.lang.String[] getAliases​(java.lang.String name)  
      java.lang.String getApplicationName()  
      org.springframework.beans.factory.config.AutowireCapableBeanFactory getAutowireCapableBeanFactory()  
      <T> T getBean​(java.lang.Class<T> requiredType)  
      <T> T getBean​(java.lang.Class<T> requiredType, java.lang.Object... objects)  
      java.lang.Object getBean​(java.lang.String name)  
      <T> T getBean​(java.lang.String name, java.lang.Class<T> requiredType)  
      java.lang.Object getBean​(java.lang.String name, java.lang.Object... args)  
      int getBeanDefinitionCount()  
      java.lang.String[] getBeanDefinitionNames()  
      java.lang.String[] getBeanNamesForAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> aClass)  
      java.lang.String[] getBeanNamesForType​(java.lang.Class type)  
      java.lang.String[] getBeanNamesForType​(java.lang.Class type, boolean includeNonSingletons, boolean allowEagerInit)  
      java.lang.String[] getBeanNamesForType​(org.springframework.core.ResolvableType resolvableType)  
      <T> org.springframework.beans.factory.ObjectProvider<T> getBeanProvider​(java.lang.Class<T> aClass)  
      <T> org.springframework.beans.factory.ObjectProvider<T> getBeanProvider​(org.springframework.core.ResolvableType resolvableType)  
      <T> java.util.Map<java.lang.String,​T> getBeansOfType​(java.lang.Class<T> type)  
      <T> java.util.Map<java.lang.String,​T> getBeansOfType​(java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)  
      java.util.Map<java.lang.String,​java.lang.Object> getBeansWithAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)  
      java.lang.ClassLoader getClassLoader()  
      java.lang.String getDisplayName()  
      org.springframework.core.env.Environment getEnvironment()  
      java.lang.String getId()  
      java.lang.String getMessage​(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, java.util.Locale locale)  
      java.lang.String getMessage​(java.lang.String code, java.lang.Object[] args, java.util.Locale locale)  
      java.lang.String getMessage​(org.springframework.context.MessageSourceResolvable resolvable, java.util.Locale locale)  
      org.springframework.context.ApplicationContext getParent()  
      org.springframework.beans.factory.BeanFactory getParentBeanFactory()  
      org.springframework.core.io.Resource getResource​(java.lang.String location)  
      org.springframework.core.io.Resource[] getResources​(java.lang.String locationPattern)  
      long getStartupDate()  
      java.lang.Class<?> getType​(java.lang.String name)  
      boolean isPrototype​(java.lang.String name)  
      boolean isSingleton​(java.lang.String name)  
      boolean isTypeMatch​(java.lang.String name, java.lang.Class targetType)  
      boolean isTypeMatch​(java.lang.String s, org.springframework.core.ResolvableType resolvableType)  
      void publishEvent​(java.lang.Object o)  
      void publishEvent​(org.springframework.context.ApplicationEvent event)  
      void putBean​(java.lang.Object bean)
      puts bean with into the context.
      void putBean​(java.lang.String name, java.lang.Object bean)
      puts bean with the given name into the context
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ApplicationContextMock

        public ApplicationContextMock()
    • Method Detail

      • putBean

        public void putBean​(java.lang.String name,
                            java.lang.Object bean)
        puts bean with the given name into the context
        Parameters:
        name -
        bean -
      • putBean

        public void putBean​(java.lang.Object bean)
        puts bean with into the context. bean object's class name will be used as the bean name.
        Parameters:
        bean -
      • getBean

        public java.lang.Object getBean​(java.lang.String name)
                                 throws org.springframework.beans.BeansException
        Specified by:
        getBean in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.BeansException
      • getBean

        public java.lang.Object getBean​(java.lang.String name,
                                        java.lang.Object... args)
                                 throws org.springframework.beans.BeansException
        Specified by:
        getBean in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.BeansException
      • getBean

        public <T> T getBean​(java.lang.String name,
                             java.lang.Class<T> requiredType)
                      throws org.springframework.beans.BeansException
        Specified by:
        getBean in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.BeansException
        See Also:
        BeanFactory.getBean(java.lang.String, java.lang.Class)
      • getBeansOfType

        public <T> java.util.Map<java.lang.String,​T> getBeansOfType​(java.lang.Class<T> type)
                                                                   throws org.springframework.beans.BeansException
        Specified by:
        getBeansOfType in interface org.springframework.beans.factory.ListableBeanFactory
        Throws:
        org.springframework.beans.BeansException
        See Also:
        ListableBeanFactory.getBeansOfType(java.lang.Class)
      • getBean

        public <T> T getBean​(java.lang.Class<T> requiredType)
                      throws org.springframework.beans.BeansException
        Specified by:
        getBean in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.BeansException
      • getBean

        public <T> T getBean​(java.lang.Class<T> requiredType,
                             java.lang.Object... objects)
                      throws org.springframework.beans.BeansException
        Specified by:
        getBean in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.BeansException
      • getBeanProvider

        public <T> org.springframework.beans.factory.ObjectProvider<T> getBeanProvider​(java.lang.Class<T> aClass)
        Specified by:
        getBeanProvider in interface org.springframework.beans.factory.BeanFactory
      • getBeanProvider

        public <T> org.springframework.beans.factory.ObjectProvider<T> getBeanProvider​(org.springframework.core.ResolvableType resolvableType)
        Specified by:
        getBeanProvider in interface org.springframework.beans.factory.BeanFactory
      • getBeansWithAnnotation

        public java.util.Map<java.lang.String,​java.lang.Object> getBeansWithAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
                                                                                      throws org.springframework.beans.BeansException
        Specified by:
        getBeansWithAnnotation in interface org.springframework.beans.factory.ListableBeanFactory
        Throws:
        org.springframework.beans.BeansException
      • findAnnotationOnBean

        public <A extends java.lang.annotation.Annotation> A findAnnotationOnBean​(java.lang.String beanName,
                                                                                  java.lang.Class<A> annotationType)
        Specified by:
        findAnnotationOnBean in interface org.springframework.beans.factory.ListableBeanFactory
      • getParent

        public org.springframework.context.ApplicationContext getParent()
        Specified by:
        getParent in interface org.springframework.context.ApplicationContext
      • getDisplayName

        public java.lang.String getDisplayName()
        Specified by:
        getDisplayName in interface org.springframework.context.ApplicationContext
      • getStartupDate

        public long getStartupDate()
        Specified by:
        getStartupDate in interface org.springframework.context.ApplicationContext
      • publishEvent

        public void publishEvent​(org.springframework.context.ApplicationEvent event)
        Specified by:
        publishEvent in interface org.springframework.context.ApplicationEventPublisher
      • publishEvent

        public void publishEvent​(java.lang.Object o)
        Specified by:
        publishEvent in interface org.springframework.context.ApplicationEventPublisher
      • containsBeanDefinition

        public boolean containsBeanDefinition​(java.lang.String beanName)
        Specified by:
        containsBeanDefinition in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeanDefinitionCount

        public int getBeanDefinitionCount()
        Specified by:
        getBeanDefinitionCount in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeanDefinitionNames

        public java.lang.String[] getBeanDefinitionNames()
        Specified by:
        getBeanDefinitionNames in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType​(org.springframework.core.ResolvableType resolvableType)
        Specified by:
        getBeanNamesForType in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType​(java.lang.Class type)
        Specified by:
        getBeanNamesForType in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType​(java.lang.Class type,
                                                      boolean includeNonSingletons,
                                                      boolean allowEagerInit)
        Specified by:
        getBeanNamesForType in interface org.springframework.beans.factory.ListableBeanFactory
      • getBeansOfType

        public <T> java.util.Map<java.lang.String,​T> getBeansOfType​(java.lang.Class<T> type,
                                                                          boolean includeNonSingletons,
                                                                          boolean allowEagerInit)
                                                                   throws org.springframework.beans.BeansException
        Specified by:
        getBeansOfType in interface org.springframework.beans.factory.ListableBeanFactory
        Throws:
        org.springframework.beans.BeansException
      • getBeanNamesForAnnotation

        public java.lang.String[] getBeanNamesForAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> aClass)
        Specified by:
        getBeanNamesForAnnotation in interface org.springframework.beans.factory.ListableBeanFactory
      • containsBean

        public boolean containsBean​(java.lang.String name)
        Specified by:
        containsBean in interface org.springframework.beans.factory.BeanFactory
      • isSingleton

        public boolean isSingleton​(java.lang.String name)
                            throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        isSingleton in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • getType

        public java.lang.Class<?> getType​(java.lang.String name)
                                   throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        getType in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • getAliases

        public java.lang.String[] getAliases​(java.lang.String name)
                                      throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        getAliases in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • getParentBeanFactory

        public org.springframework.beans.factory.BeanFactory getParentBeanFactory()
        Specified by:
        getParentBeanFactory in interface org.springframework.beans.factory.HierarchicalBeanFactory
      • getMessage

        public java.lang.String getMessage​(java.lang.String code,
                                           java.lang.Object[] args,
                                           java.lang.String defaultMessage,
                                           java.util.Locale locale)
        Specified by:
        getMessage in interface org.springframework.context.MessageSource
      • getMessage

        public java.lang.String getMessage​(java.lang.String code,
                                           java.lang.Object[] args,
                                           java.util.Locale locale)
                                    throws org.springframework.context.NoSuchMessageException
        Specified by:
        getMessage in interface org.springframework.context.MessageSource
        Throws:
        org.springframework.context.NoSuchMessageException
      • getMessage

        public java.lang.String getMessage​(org.springframework.context.MessageSourceResolvable resolvable,
                                           java.util.Locale locale)
                                    throws org.springframework.context.NoSuchMessageException
        Specified by:
        getMessage in interface org.springframework.context.MessageSource
        Throws:
        org.springframework.context.NoSuchMessageException
      • getResources

        public org.springframework.core.io.Resource[] getResources​(java.lang.String locationPattern)
                                                            throws java.io.IOException
        Specified by:
        getResources in interface org.springframework.core.io.support.ResourcePatternResolver
        Throws:
        java.io.IOException
      • getResource

        public org.springframework.core.io.Resource getResource​(java.lang.String location)
        Specified by:
        getResource in interface org.springframework.core.io.ResourceLoader
      • getAutowireCapableBeanFactory

        public org.springframework.beans.factory.config.AutowireCapableBeanFactory getAutowireCapableBeanFactory()
                                                                                                          throws java.lang.IllegalStateException
        Specified by:
        getAutowireCapableBeanFactory in interface org.springframework.context.ApplicationContext
        Throws:
        java.lang.IllegalStateException
      • containsLocalBean

        public boolean containsLocalBean​(java.lang.String arg0)
        Specified by:
        containsLocalBean in interface org.springframework.beans.factory.HierarchicalBeanFactory
      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
        Specified by:
        getClassLoader in interface org.springframework.core.io.ResourceLoader
      • getId

        public java.lang.String getId()
        Specified by:
        getId in interface org.springframework.context.ApplicationContext
      • getApplicationName

        public java.lang.String getApplicationName()
        Specified by:
        getApplicationName in interface org.springframework.context.ApplicationContext
      • isPrototype

        public boolean isPrototype​(java.lang.String name)
                            throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        isPrototype in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • isTypeMatch

        public boolean isTypeMatch​(java.lang.String s,
                                   org.springframework.core.ResolvableType resolvableType)
                            throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        isTypeMatch in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • isTypeMatch

        public boolean isTypeMatch​(java.lang.String name,
                                   java.lang.Class targetType)
                            throws org.springframework.beans.factory.NoSuchBeanDefinitionException
        Specified by:
        isTypeMatch in interface org.springframework.beans.factory.BeanFactory
        Throws:
        org.springframework.beans.factory.NoSuchBeanDefinitionException
      • getEnvironment

        public org.springframework.core.env.Environment getEnvironment()
        Specified by:
        getEnvironment in interface org.springframework.core.env.EnvironmentCapable