0

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

nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • But why do you want to do this? you should only need to SUT and maybe some test doubles to be retrieved from the context. It seems you are doing something else (probably with other better solution) now – Adrian Shum Nov 25 '15 at 09:47
  • I have round about 500 hundret service beans in the context. I want to test the start/stop funtionality about a list of Services without wiring each one seperatly. – nano_nano Nov 25 '15 at 10:14
  • Personally I wouldn't use `SpringJUnit4ClassRunner` + `ContextConfiguration` in such case. Just make a normal unit test, create the app context using whatever xml you want and test against it. I have done something similar as what you did (loading my application's app context in a unit test and verify the status of it) and turned out that there were some problems when I was using `@ContextConfiguration` (cannot recall right now) – Adrian Shum Nov 26 '15 at 01:54

1 Answers1

1

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());
   }
}
schrieveslaach
  • 1,689
  • 1
  • 15
  • 32