More clarification, this method is not using Spring component scan anymore, it uses xml file to load beans in which the xml file is generated on demand.
> -----Original Message----- > From: Frank Zhang > Sent: Friday, January 25, 2013 5:54 PM > To: cloudstack-dev@incubator.apache.org > Subject: RE: Writing unit tests after javelin is merged in. > > > Frank, > > > > Great! Two questions. > > > > - How does it work when the unit test itself needs a component injected? > > Setup() is only called before the test but not before the unit test is > initialized. > > The components in unit case should be gotten by > ComponentContext.getComponent() call, not by Spring injection. > That means the unit test case itself is not Spring aware of. > > For example, to get UserVmDao the code then would look like: > > Public class MyTestCase() { > private UserVmDao vmDao; > > @Before > Public void setup() throws Exception { > ComponentContext.setComponentsToLoad(Class<?>[] > { SecurityGroupRulesDaoImpl.class, > UserVmDaoImpl.class, > AccountDaoImpl.class,}); > > ComponentContext. initComponentsLifeCycle(); > > vmDao = > ComponentContext.getComponent(UserVmDaoImpl.class) > > } > } > > > - What about the aop extras that Kelven has added in the xml file? I > > don't see them specified in the setup() method. How does that get loaded? > > > > Configuration.xml you post is as a template, so it would contains all > necessary > beans including extra stuff like AOP. > When creating new spring xml file, ComponentContext copies the > Configuration.xml and inject new beans declarations into it. > Then the final xml file will contains all components needed > > > > Thanks. > > > > --Alex > > > > > -----Original Message----- > > > From: Frank Zhang [mailto:frank.zh...@citrix.com] > > > Sent: Friday, January 25, 2013 5:06 PM > > > To: cloudstack-dev@incubator.apache.org > > > Subject: RE: Writing unit tests after javelin is merged in. > > > > > > I think the former mail is too verbose. Let me summarize: > > > > > > For a unit test case, all necessary to do is: > > > > > > @Before > > > public void setup() throws Exception { > > > ComponentContext.setComponentsToLoad(Class<?>[] > > > { SecurityGroupRulesDaoImpl.class, > > > UserVmDaoImpl.class, > > > AccountDaoImpl.class,}); > > > > > > ComponentContext. initComponentsLifeCycle(); } > > > > > > To achieve this. Dynamically generate spring xml configure file > > > including beans specified in setComponentsToLoad(), use the > > > generated file to create Spring ApplicationContext. > > > > > > > > > > > > > -----Original Message----- > > > > From: Frank Zhang [mailto:frank.zh...@citrix.com] > > > > Sent: Friday, January 25, 2013 4:59 PM > > > > To: cloudstack-dev@incubator.apache.org > > > > Subject: RE: Writing unit tests after javelin is merged in. > > > > > > > > Alex, I feel the wiki you post is too complex to write a unit case. > > > > If I understand correctly, the whole purpose about > > > > configuration.xml and "test configuration" class is to load > > > > components needed by unit test > > case. > > > > From a programmer's perspective, all things I think are necessary > > > > to do is > > > like > > > > below: > > > > > > > > @Before > > > > public void setup() throws Exception { > > > > ComponentContext.setComponentsToLoad(Class<?>[] > > > > { SecurityGroupRulesDaoImpl.class, > > > > UserVmDaoImpl.class, > > > > AccountDaoImpl.class,}); > > > > ComponentContext. initComponentsLifeCycle(); } > > > > > > > > For these mock component, test case itself should know where to > > > > get it, > > > for > > > > example, by calling Mockito.mock(). > > > > > > > > To achieve this, ComponentContext needs below enhancements: > > > > 1. add method setComponentsToLoad() that receives a list of > > > > Class<?> of components 2. save configuration.xml to a well-know > > > > place in > > classpath. > > > The > > > > content of configuration.xml is 99% the same as you post, except > > removing: > > > > > > > > <bean id="TestConfiguration" > > > > class="[Test Configuration]" /> > > > > > > > > then the file works as a template for Spring beans. > > > > > > > > 3. ComponentContext generates a new xml file from > > > > configuration.xml template when initComponentsLifeCycle() gets > > > > called, the new xml file would contain all bean declarations for > > > > classes specified in setComponentsToLoad(). as aforementioned > > > > example, the new generated xml will contain extra 3 items like > > > > > > > > <bean name="SecurityGroupRulesDaoImpl" > class="full_class_name_of_ > > > > SecurityGroupRulesDaoImpl" /> <bean name=" UserVmDaoImpl " > > > > class="full_class_name_of_UserVmDaoImpl" /> <bean name=" > > > > AccountDaoImpl " class="full_class_name_of_AccountDaoImpl " /> > > > > > > > > 4. save the new xml file generated in step 3 to root class path of > > > > unit test case with a new name. For standard maven it is > > > > test/target/test-classes/, > > > but > > > > I didn't see this hierarchy in our code base, let's assume the > > > > path is test/classes/. So the new xml file would be saved as: > > > > > > > > test/classes/spring_configuration_for_test_case_xxx.xml > > > > > > > > 5. create Spring ApplicationContext using the new generated > > > > configuration file in initComponentsLifeCycle(). > > > > > > > > 6. don't use @ContextConfiguration(locations = > > > > "classpath:/[configuration.xml]") as we use dynamic Spring > > > > configuration > > > > > > > > > > > > > > > > > > > > > > > > > -----Original Message----- > > > > > From: Alex Huang [mailto:alex.hu...@citrix.com] > > > > > Sent: Friday, January 25, 2013 10:25 AM > > > > > To: cloudstack-dev@incubator.apache.org > > > > > Subject: RE: Writing unit tests after javelin is merged in. > > > > > > > > > > Hi Howie, > > > > > > > > > > Thanks for the offer to help. I believe we finally converted > > > > > all tests yesterday but turns out the tests are not running > > > > > because they've been disabled on master branch due to no db > > > > > access on the build > > > > machines. > > > > > > > > > > The code is on javelin on asf. You're welcome to peruse and > > > > > make any suggestions about how we're using spring in the unit tests. > > > > > Would love to find a way to stop creating 3 different files for > > > > > every unit > > test. > > > > > > > > > > Thanks again. > > > > > > > > > > --Alex > > > > > > > > > > > -----Original Message----- > > > > > > From: Howie Yu [mailto:howie...@trend.com.tw] > > > > > > Sent: Thursday, January 24, 2013 5:28 PM > > > > > > To: cloudstack-dev@incubator.apache.org > > > > > > Subject: Re: Writing unit tests after javelin is merged in. > > > > > > > > > > > > Hi Alex > > > > > > > > > > > > I would like to help~ > > > > > > > > > > > > But which repository should I use? Form github's javelin or > > > > > > asf's > > javelin ? > > > > > > > > > > > > > > > > > > On 13/1/25 上午8:17, "Alex Huang" <alex.hu...@citrix.com> > wrote: > > > > > > > > > > > > >I wrote up a page on how to write unit tests after the Spring > > > > > > >injection framework is in. Please see here. > > > > > > > > > > > > > >https://cwiki.apache.org/confluence/display/CLOUDSTACK/Unit+T > > > > > > >e > > s > > > > > > >ti > > > n > > > > g > > > > > + > > > > > > with+J > > > > > > >Unit+and+Spring > > > > > > > > > > > > > >We also need help in javelin to finish converting the unit > > > > > > >tests over to this format. If you can help, let me know. > > > > > > > > > > > > > >Thanks. > > > > > > > > > > > > > >--Alex > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > <table class="TM_EMAIL_NOTICE"><tr><td><pre> > > > > > > TREND MICRO EMAIL NOTICE > > > > > > The information contained in this email and any attachments is > > > > > > confidential and may be subject to copyright or other > > > > > > intellectual property > > > > > protection. > > > > > > If you are not the intended recipient, you are not authorized > > > > > > to use or disclose this information, and we request that you > > > > > > notify us by reply mail or telephone and delete the original > > > > > > message from your mail > > > > > system. > > > > > > </pre></td></tr></table>