|
From: Daniel B. <dan...@gm...> - 2008-05-23 11:50:55
|
Hi,
Yep, you could do that, but then you've done the hard work of
identifying which constructor to use outside of the ReflectionFacade,
and I believe it belongs on the inside.
I would probably do
Object createInstanceByNameAndParameters(String className, Object[] parameters)
("create" better reflects what the method actually does, and should be
applied on the method below as well)
Cheers
Daniel
On Fri, May 23, 2008 at 9:44 AM, Ziem, Thomas <Tho...@t-...> wrote:
> Hi Daniel,
>
> that sounds good.
>
> What's your suggestion for non-default constructors?
> Perhaps another function like that:
>
> Object getInstanceByConstructor(Constructor constructor)
>
> Regards,
> Thomas
>
>> -----Ursprüngliche Nachricht-----
>> Von: rmo...@li...
>> [mailto:rmo...@li...] Im Auftrag
>> von Daniel Brolund
>> Gesendet: Donnerstag, 22. Mai 2008 19:42
>> An: rmo...@li...
>> Betreff: Re: [Rmock-users] RMock and Java Reflection API
>>
>> The variables "name" and "className" should of course be the
>> same, my bad.
>>
>> Cheers
>> Daniel
>>
>> On Thu, May 22, 2008 at 7:39 PM, Daniel Brolund
>> <dan...@gm...> wrote:
>> > Hi Thomas,
>> >
>> > Javas reflection classes are generally not "mockable".
>> >
>> > What I have done successfully in several project was to create a
>> > facade with the needed functionality.
>> >
>> > public class ReflectionStuff {
>> > Class getClassByName(String name) {
>> > try {
>> > return Class.forName(className);
>> > } catch(EveryExceptionKnownToMan e) {
>> > //handle nicely by throwing e.g. a runtime exception } } Object
>> > getInstanceByName(String name) { return
>> > getClassByName(name).newInstance(); // You need to catch a
>> few more
>> > here :-P } }
>> >
>> > Then you can mock that class (or create an interface in the same
>> > manner if you prefer that) and inject into the class you
>> are testing.
>> >
>> > A good bonus is that you will handle all of the meaningless
>> exceptions
>> > Java might throw when reflecting in one place in the code.
>> >
>> > Did that help?
>> >
>> > Cheers
>> > Daniel
>> >
>> > On Thu, May 22, 2008 at 6:29 PM, Ziem, Thomas
>> <Tho...@t-...> wrote:
>> >> Hi all,
>> >>
>> >> I want to test a class which loads classes using the Java
>> Reflection
>> >> API:
>> >>
>> >> Class.forName(className).newInstance();
>> >>
>> >> The value of className comes from a configuration property.
>> >> Is there any way that the class under test uses a mocked
>> object instead?
>> >>
>> >> Thank's,
>> >> Thomas
>> >>
>> >>
>> ---------------------------------------------------------------------
>> >> ---- This SF.net email is sponsored by: Microsoft Defy all
>> >> challenges. Microsoft(R) Visual Studio 2008.
>> >> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>> >> _______________________________________________
>> >> Rmock-users mailing list
>> >> Rmo...@li...
>> >> https://lists.sourceforge.net/lists/listinfo/rmock-users
>> >>
>> >
>> >
>> >
>> > --
>> > __________________________
>> > Dan...@Gm...
>> >
>>
>>
>>
>> --
>> __________________________
>> Dan...@Gm...
>>
>> --------------------------------------------------------------
>> -----------
>> This SF.net email is sponsored by: Microsoft Defy all
>> challenges. Microsoft(R) Visual Studio 2008.
>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>> _______________________________________________
>> Rmock-users mailing list
>> Rmo...@li...
>> https://lists.sourceforge.net/lists/listinfo/rmock-users
>>
>
--
__________________________
Dan...@Gm...
|