1

I want to use a specific java bean to create test cases from an outside list. I'm trying to find a java tool (spring framework? or similar) to instantiate this bean and populate it with values from that outside list (I will do the loop). (some kind of bean injection) Do you know any tool for this job?

thanks, José Cruz

José Cruz
  • 35
  • 3

3 Answers3

3

I'm a big Spring fan, but I don't think that Spring will be doing you much good in this case. I'd create that list of test beans in a loop when setting up your test and be done with it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

I am not a fan of Spring. Go for Guice or Weld for Dependency Injection.

In your case, it sounds like that it will be much easier to use Commons BeanUtils. They have a populate method with following signature, which use a Map instead of a List.

public static void populate(Object bean,
                            Map properties)
                     throws IllegalAccessException,
                            InvocationTargetException

Reference: BeanUtils.populate(Object bean, Map properties)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Puspendu Banerjee
  • 2,631
  • 16
  • 19
  • Of course I should give you -1 for the first paragraph (Spring rülez) :-) But here's +1 for your BeanUtils solution – Sean Patrick Floyd Dec 29 '10 at 10:01
  • @Sean The way SpingSource has changed its Licensing model [read revenue model] is very annoying. Secondly convention over configuration always won. Most of the features are readily available and easily implementable on Java EE 6. Then why something else? Last, but not the least,why should you use such a large framework to do a simple thing? :) – Puspendu Banerjee Dec 29 '10 at 17:19
  • I agree with the last part, BeanWrapper should be a standalone technology just like BeanUtils. But the whole point of Spring is that you don't need Java EE. You get the full power even in a servlet container or standalone app – Sean Patrick Floyd Dec 29 '10 at 19:57
  • @Sean The support issue may be important as well. It is more and more important to provide the support for the full stack from one company. I guess in future you will see SpringSource only (e.g. dm server), and Java EE 6 only solutions. These decisions will be not driven by technical, rather than political / strategic reasons. Imagine Spring will run on a commercial application server in production and something goes wrong? – Puspendu Banerjee Dec 30 '10 at 17:35
0

Basically, what all these frameworks use internally is the BeanInfo / Introspector mechanism. So you can of course use this mechanism yourself if you don't want to do it yourself.

See this previous answer of mine for a similar solution:
How to set the fields of an object given a map of key/values?

But of course with Spring this kind of thing is much more comfortable, especially if you use the BeanWrapper technology (which of course does pretty much the same thing internally in a fancier, more extensible way).

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588