Results 1 to 7 of 7

Thread: Access to @Component beans in unit test using AnnotationConfigWebApplicationContext

  1. #1
    Join Date
    Oct 2007
    Posts
    5

    Default 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. #2

    Default 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

  3. #3
    Join Date
    Oct 2007
    Posts
    5

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You don't tell the AnnotationApplicationContext where to find your @Configuration files, without that well nothing is loaded...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Oct 2007
    Posts
    5

    Default

    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.

  6. #6
    Join Date
    Oct 2007
    Posts
    5

    Default

    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);
    	}
    }

  7. #7

    Default 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 at 03:17 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •