feature: mocking annotations possible?

3,924 views
Skip to first unread message

manuel aldana

unread,
Oct 31, 2010, 7:46:50 AM10/31/10
to moc...@googlegroups.com
Hi,

is there a way to mock annotations with mockito? So far couldn't find
this in api. I would like to use this, because I have some pieces of
code, which is doing a lot of logic based on given annotations.

Maybe this hasn't been implemented yet, because there is jvm restriction
on making annotations more flexible during runtime?

--
manuel aldana
ald...@gmx.de
software-engineering blog: http://www.aldana-online.de

szczepiq

unread,
Oct 31, 2010, 7:53:44 AM10/31/10
to moc...@googlegroups.com
Can you show us how would you see it working? (code sample would be good :)

Cheers,
Szczepan


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


Felix Leipold

unread,
Nov 1, 2010, 12:58:59 PM11/1/10
to moc...@googlegroups.com
If the logic is driven by annotations, why not have a hand-rolled test class with that annotation? If this is not possible, what (dependency) is stopping you? That might be an indicator where to change your design.

To put it more bluntly: If you want to mock annotations, chances are that you are about to commit a terrible atrocity.
;-)

manuel aldana

unread,
Nov 1, 2010, 4:49:22 PM11/1/10
to moc...@googlegroups.com
yes static class inside unit-test is what I use currently, but it bloats code (it implements an interface). It is similar effort as if I would need to implement "normal" interface or class mocks.

You're right one shouldn't overdo annotation usage, but there are cases (e.g. framework code/annotations) where they are the best solution. I need to test this also.

Bryce

unread,
Nov 1, 2010, 5:21:58 PM11/1/10
to mockito
Hello Manuel,

Actually I'm thinking about annotations on the method or on the
parameters, am I correct ?

Are you looking for something like this:

TheSpecifiedInterface annotationEnhancedMock =
Mockito.mockBuilder().forClass(TheSpecificInterface.class)
.withMethodEnhancer(enhancer("methodName").withMethodAnnotation(MethodAnnotation.class))
.withMethodEnhancer(enhancer("anotherMethod", String.class,
Integer.class).withMethodAnnotation(MethodAnnotation.class).withParameterAnnotation(AnotherAnnotation.class,
null))
.mock();

It certainly is not the more concise way. And it's probably as much
verbose as reimplementing it in your code. If the actual re-
implementaion bloats the code, maybe your complexity is elsewhere. You
might want to split your objects with smaller responsibility, your
interfaces with fewer concepts.

Anyway as Szczepan said, please give us code sample :)




--
Brice



On Nov 1, 9:49 pm, manuel aldana <ald...@gmx.de> wrote:
> yes static class inside unit-test is what I use currently, but it bloats
> code (it implements an interface). It is similar effort as if I would
> need to implement "normal" interface or class mocks.
>
> You're right one shouldn't overdo annotation usage, but there are cases
> (e.g. framework code/annotations) where they are the best solution. I
> need to test this also.
>
> On 01/11/10 17:58, Felix Leipold wrote:
>
>
>
>
>
>
>
>
>
> > If the logic is driven by annotations, why not have a hand-rolled test
> > class with that annotation? If this is not possible, what (dependency)
> > is stopping you? That might be an indicator where to change your design.
>
> > To put it more bluntly: If you want to mock annotations, chances are
> > that you are about to commit a terrible atrocity.
> > ;-)
>
> > On Sun, Oct 31, 2010 at 11:53 AM, szczepiq <szcze...@gmail.com
> > <mailto:szcze...@gmail.com>> wrote:
>
> >     Can you show us how would you see it working? (code sample would
> >     be good :)
>
> >     Cheers,
> >     Szczepan
>
> >     On Sun, Oct 31, 2010 at 12:46 PM, manuel aldana <ald...@gmx.de
> >     <mailto:ald...@gmx.de>> wrote:
>
> >         Hi,
>
> >         is there a way to mock annotations with mockito? So far
> >         couldn't find this in api. I would like to use this, because I
> >         have some pieces of code, which is doing a lot of logic based
> >         on given annotations.
>
> >         Maybe this hasn't been implemented yet, because there is jvm
> >         restriction on making annotations more flexible during runtime?
>
> >         --
> >          manuel aldana
> >         ald...@gmx.de <mailto:ald...@gmx.de>
> >          software-engineering blog:http://www.aldana-online.de
>
> >         --
> >         You received this message because you are subscribed to the
> >         Google Groups "mockito" group.
> >         To post to this group, send email to moc...@googlegroups.com
> >         <mailto:moc...@googlegroups.com>.
> >         To unsubscribe from this group, send email to
> >         mockito+u...@googlegroups.com
> >         <mailto:mockito%2Bunsu...@googlegroups.com>.
> >         For more options, visit this group at
> >        http://groups.google.com/group/mockito?hl=en.
>
> >     --
> >     You received this message because you are subscribed to the Google
> >     Groups "mockito" group.
> >     To post to this group, send email to moc...@googlegroups.com
> >     <mailto:moc...@googlegroups.com>.
> >     To unsubscribe from this group, send email to
> >     mockito+u...@googlegroups.com
> >     <mailto:mockito%2Bunsu...@googlegroups.com>.

szczepiq

unread,
Nov 2, 2010, 5:09:11 AM11/2/10
to moc...@googlegroups.com
>yes static class inside unit-test is what I use currently, but it bloats code (it implements an interface).

Try to write a smallest possible class that has the annotation you need. Then test this class. If the interface is big, don't make this class implement this interface ;)

Cheers!
Szczepan
Reply all
Reply to author
Forward
0 new messages