Constructor args for [Mock] metadata

667 views
Skip to first unread message

loomis

unread,
Feb 25, 2011, 5:00:15 PM2/25/11
to mockito-flex
Hi,

I got a question on how to provide constructor args for mocks assigned
by [Mock] metadata.

One can provide a mock name via name parameter as well as the
constructor arguments if required via argsList parameter that should
be a name of the public field/property of the test class that holds an
Array of the required parameters.

Here is a simple example:

public class MockAssignment
{
[Rule]
public var mockitoRule:IMethodRule = new MockitoRule();

[Mock(type="org.mockito.MockieWithArgs",
argsList="constructorArgs")]
public var mockieWithArgs:MockieWithArgs;

public var constructorArgs:Array = ["MyNameIs"]; // this can be a
property as well
}

Cheers,
Kris

Ruth

unread,
Feb 28, 2011, 3:39:20 AM2/28/11
to mockito-flex
Hi, thanks for this solution.

Is there also any solution where I don't need to pass any constructor
arguments and they will be simply ignored or set to null?
Where can I find the documentation about what attributes can the
metadata tag Mock take? (or where in the source code?)

Regards,

Ruth

Johanna

unread,
Feb 28, 2011, 3:52:04 AM2/28/11
to mockito-flex
Hi,

Thanks for your reply.

Is there any way to ignore the constructor args, so that I don't have
to specify them every time? I am almost never interested in the
constructor args, and it is quite a hassle to pass them anytime I mock
something...

Regards,

Johanna

On Feb 25, 11:00 pm, loomis <kris.karczmarc...@gmail.com> wrote:

Kris

unread,
Feb 28, 2011, 4:30:36 AM2/28/11
to mockit...@googlegroups.com
Unfortunately, due to AVM implementation and the object model it's not possible. The byte code generated class needs to extend the mocked type thus it calls a super class constructor. AVM does not allow creating instances other than calling the constructor.

As far as the documentation - the only place at the moment are the release notes for 1.3 and below description. If you are interested in writing something up please let me know.

At the moment [Mock] annotation uses  these parameters

type - class to mock
mockName - optional mock instance name,
argsList - public field or property that holds array with arguments


The class (a bit dirty) that handles metadata is here:

https://bitbucket.org/loomis/mockito-flex/src/cf073a275cea/mockito/src/main/flex/org/mockito/integrations/flexunit4/MockitoMetadataTools.as

Hope this helps,
Kris

Johanna

unread,
Feb 28, 2011, 6:42:42 AM2/28/11
to mockito-flex
Hi,

OK, so you must call the constructor with all arguments... but would
it not be possible to pass default constructor values to the code that
generates the bytecode? I am not sure if you can get that information
via reflection before calling the constructor...


Regards,

Johanna

On Feb 28, 10:30 am, Kris <kris.karczmarc...@gmail.com> wrote:
> Unfortunately, due to AVM implementation and the object model it's not
> possible. The byte code generated class needs to extend the mocked type thus
> it calls a super class constructor. AVM does not allow creating instances
> other than calling the constructor.
>
> As far as the documentation - the only place at the moment are the release
> notes for 1.3 and below description. If you are interested in writing
> something up please let me know.
>
> At the moment [Mock] annotation uses  these parameters
>
> type - class to mock
> mockName - optional mock instance name,
> argsList - public field or property that holds array with arguments
>
> The class (a bit dirty) that handles metadata is here:
>
> https://bitbucket.org/loomis/mockito-flex/src/cf073a275cea/mockito/sr...
>
> Hope this helps,
> Kris

Kris

unread,
Feb 28, 2011, 7:04:58 AM2/28/11
to mockit...@googlegroups.com
I'm not quite sure how this would work. You'd have to tell somewhere (potentially on each test that uses this type) what default args to use. Not sure what is different here from what we already have.

As far as the reflection thing it's really poor on constructors in flex.

Also you don't have to assign mocks automatically. When you put [Mock(type="mockedType")] above the test class instead of the field in it, mockito will only generate class for you but you'll have to assign mocks manually.
Then you can create a utility function that creates mock instance for you with a desired parameters and reuse it in your tests.

package i.love.constructor.args
{
[Mock(type="i.love.constructor.args.Collaborator")]
public class MyTest
{
     [Test]
     public function shouldPass():void
     {
         // given
         var collaborator:Collaborator = bakeCollaboratorWithDefaultArgs();
         .....
     }
}
}

package i.love.constructor.args
{
    public function bakeCollaboratorWithDefaultArgs():Collaborator
    {
         return mock(Collaborator, null, [ "all the required parameters" ]);
    }
}

Regards,
Kris

Johanna

unread,
Feb 28, 2011, 7:57:07 AM2/28/11
to mockito-flex
Hi Kris,

Thanks for the reply. What I mean with default arguments is passing
null for objects, 0 for numbers and false for booleans. If you could
find via reflection what are the constructor arguments, and then pass
these values depending on the type, it would help a lot if the mock
object could be constructed automatically without the need to define
the arguments manually... But I don't know the inner guts of Mockito,
so I don't know what is possible...


Regards,

Johanna
Reply all
Reply to author
Forward
0 new messages