Search code examples
javaspringjunitspringjunit4classrunnercontext-configuration

get all Beans of type in SpringJUnit4ClassRunner


I have problems to find a suitable answer for my following test class:

@ContextConfiguration("classpath:services.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class RunnableServiceTest {

   @Test
   public void testConfiguration(){
       Collection<Service> lAllService = >>getBeansOfType(Service.class)<<;
       assertFalse(lAllService.isEmpty());
   }
}

I want to collect all Spring managed beans from the bounded context services.xml that are type of Service.

I am pretty sure there must be something like this but I dont know what I have to search for.

Thx a lot for your help.

Stefan


Solution

  • You can use an autowired List

    @ContextConfiguration("classpath:services.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class RunnableServiceTest {
    
       @Autowired
       private List<Service> lAllService;
    
       @Test
       public void testConfiguration(){
           assertFalse(lAllService.isEmpty());
       }
    }