Announcement Announcement Module
Collapse
No announcement yet.
Access to @Component beans in unit test using AnnotationConfigWebApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access to @Component beans in unit test using AnnotationConfigWebApplicationContext

    Hi, I am trying to write a simple junit test that loads up one of my annotation based components/beans but I am not being successful when trying to use the AnnotationConfigWebApplicationContext class to do this.

    In my test class, I am instantiating a new AnnotationConfigWebApplicationContext, calling refresh() in my @Before method (it tells me to do this) and within my test, I am calling:

    Code:
    public class HighriseClientTest {
    	HighriseClient client;
    	AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
    	
    	@Before
    	public void setup() {
    		annotationConfigWebApplicationContext.refresh();
    	}
    	
    	@Test
    	public void testGetPeople() throws Exception {
    		String[] beanNamesForType = annotationConfigWebApplicationContext.getBeanNamesForType(HighriseClient.class);
    		client = annotationConfigWebApplicationContext.getBean(HighriseClient.class);
    		
    		List<Contact> people = client.getPeople();
    		assertNotNull(people);
    	}
    }
    I do not get any values in array beanNamesForType nor am I able to find the bean I am looking for by Type or literal name (I even tried specifying a Qualifier)

    Any help would be greatly appreciated.

  • #2
    hope this helps

    Which version of Spring are you using?

    In case you specify configuration by using xml (in spring 2.5.6)
    you could annotate your testclass with something like

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
    	"classpath:/my-first-config.xml", 
    	"classpath:/my-second-config.xml"
    })
    so you would not need the applicationContext as variable

    Comment


    • #3
      Oops, forgot to mention the Spring version. I am using Spring 3.0.0RC3. I will try what you suggested, but the Bean I am trying to retrieve is no defined in my config files, but rather defined using @Component annotation. Will your method pick those up as well? I guess there's only one way to find out.. I'll try it out and report back.

      Thanks.

      Comment


      • #4
        You don't tell the AnnotationApplicationContext where to find your @Configuration files, without that well nothing is loaded...

        Comment


        • #5
          I don't have any @Configuration classes defined in my project. I simply have a few xml config files loaded through web.xml. By @Configuration, do you mean those defined by xml files as well? Sorry, a little new to all this Spring annotation stuff.

          Here is my new code, but I am still failing to load the annotated component/bean that I need to do my testing:

          Code:
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(locations = {
          	"classpath:/conf/mebuyer-mvc.xml"
          })
          public class HighriseClientTest {
          	HighriseClient client;
          	AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
          	
          	@Before
          	public void setup() {
          		annotationConfigWebApplicationContext.refresh();
          	}
          	
          	@Test
          	public void testGetPeople() throws Exception {
          		String[] beanNamesForType = annotationConfigWebApplicationContext.getBeanNamesForType(HighriseClient.class);
          		client = annotationConfigWebApplicationContext.getBean(HighriseClient.class);
          		
          		List<Contact> people = client.getPeople();
          		assertNotNull(people);
          	}
          }
          I have other config files besides mebuyer-mvc.xml, but I get some errors when trying to load those in. Most of what I need is contained within the mvc config file.

          Comment


          • #6
            I think I just figured it out .. getting a different unrelated error message now, but I am actually able to get the bean!

            I understand how this stuff works now. thanks for the help. high five.

            Code:
            @RunWith(SpringJUnit4ClassRunner.class)
            @ContextConfiguration(locations = {
            	"classpath:/conf/mebuyer-mvc.xml"
            })
            public class HighriseClientTest {
            	@Autowired
            	HighriseClient client;
            	
            	@Before
            	public void setup() {
            	}
            	
            	@Test
            	public void testGetPeople() throws Exception {
            		List<Contact> people = client.getPeople();
            		assertNotNull(people);
            	}
            }

            Comment


            • #7
              might work as well

              also if you want to use the AnnotationConfigWebApplicationContext (as one of the replies suggested) and annotated classes as config refer to http://static.springsource.org/sprin...r-contstructor
              Last edited by fdemilde; Dec 16th, 2009, 03:17 PM.

              Comment

              Working...
              X