junit - how can I inject "nulls" into autowired spring beans? -


i writing unit-tests , have quite complex setting.

a dependent bean sets listeners , passes them autowired services.

i want test listeners present, not call them, want pass 'null' instead of autowired service. (specifically: not have setters ...)

@autowired someservice1 service1  @autowired someservice2 service2   public list getlisteners() {   list l = new arraylist();   l.add(new aaalistener(service1));   l.add(new bbblistener(service2));   return l; } 

@test public void testlisteners() {   int exptecedsize = 2;    sut.dosomething();    list l = sut.getx().gety().getlisteners()    assertequals(expectedsize,l.size()); } 

note sut depend indirectly class returns listeners.

since small example big setting, do not want use mocks here want test presence not behavior of listeners.

mocking 20 or 30 of such services slow down tests massively.

question: easiest way inject these nulls autowired instance variables?

a) add setters ?

b) reflectionutils ?

c) java-config + @beans + return null ?

they're null when class instantiated ... or running them in spring context?

you can set properties null in xml config (from documentation)

<bean class="examplebean">     <property name="email"><null/></property> </bean> 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -