Announcement Announcement Module
Collapse
No announcement yet.
How to load webApplicationContext in jUnit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to load webApplicationContext in jUnit

    Hi I need to get a bean from my spring webApplicationContext with the injected property values. How do I access the bean in my Test*** class ( I am using JUnit ) ?

  • #2
    solved it myself.

    Code:
    import com.amg.brides.util.test.BridesBaseSpringTest;
    import com.amg.brides.util.yosemite.AbstractFileFilter;
    import org.springframework.beans.factory.annotation.Autowired;
    import junit.framework.JUnit4TestAdapter;
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.web.context.support.*;
    import org.springframework.web.context.*;
    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.*;
    import org.springframework.core.io.DefaultResourceLoader;
    import org.springframework.core.io.Resource;
    
    
    public class  TestHomepageFileFilter extends BridesBaseSpringTest {
    
        protected ClassPathXmlApplicationContext appContext;
        protected HomepageFileFilter homepageFileFilter;
    
        public TestHomepageFileFilter(){
           String[] contextFiles = {"spring-yosemite.xml"};
           appContext = new ClassPathXmlApplicationContext(contextFiles);
        }
    
        @Test
        public void testInjectedPropertyValues() {
            homepageFileFilter = (HomepageFileFilter)appContext.getBean("homepageFileFilter");
            System.out.println("in TestHomepageFileFilter :: testInjectedPropertyValues() -START");
            System.out.println(" Properties from parent class : ");
            System.out.println(" getXmlDir() is : "+homepageFileFilter.getXmlDir());
            System.out.println(" getFileNamePattern() is : "+homepageFileFilter.getFileNamePattern());
            System.out.println(" getTimeFormatInFileName() is : "+homepageFileFilter.getTimeFormatInFileName());
    
            System.out.println(" Properties from HomepageFileFile : ");
            System.out.println(" getXmlSubDir() is : "+ homepageFileFilter.getXmlSubDir());
            System.out.println(" getFileNamePrefix() is : "+homepageFileFilter.getFileNamePrefix());
        }
    
        @Test
        public void testgetLatestVersion() throws Exception {
            System.out.println("#testGetLatestVersion() starts");
            homepageFileFilter = (HomepageFileFilter)appContext.getBean("homepageFileFilter");
            assertNotNull(homepageFileFilter.getLatestVersion("/"));
        }
    
        @Test
        public void testisPublished() throws Exception {
        }

    Comment


    • #3
      Originally posted by adellalin View Post
      Hi I need to get a bean from my spring webApplicationContext with the injected property values. How do I access the bean in my Test*** class ( I am using JUnit ) ?
      8. Testing

      Comment

      Working...
      X