Testing AOP and method names

387 views
Skip to first unread message

Dirk Nimerem

unread,
Dec 26, 2012, 3:15:12 PM12/26/12
to google...@googlegroups.com
Hi,

Let's say i have a class which represents an user, called "User" and an interface implemented by this class called "IUser". The IUser interface declares one method called "sendMail(String text)" for the user. Also i created an interceptor/ aspect via the Guice bindInterceptor method for my IUser. The aspect simply logs every call for the sendMail() method, that works fine.

1.) But how do i test, that my aspect works properly? I don't have an idea how to write an unit test for that. My idea was to create an IUser mock and trigger the public sendMail() method from outside. But how do I create an aspect on top of a mock?

2.) In my aspect/ interceptor i simply check the methodname like this: "if methodname == 'sendMail'". But if i rename/ refactor the sendMail Method my aspect won't work anymore. Is there a better way to to that?

Thanks,
Dirk

Jordi Gerona

unread,
Dec 28, 2012, 5:38:19 AM12/28/12
to google...@googlegroups.com
On Wed, Dec 26, 2012 at 9:15 PM, Dirk Nimerem <nene...@sinnlos-mail.de> wrote:
Hi,

Let's say i have a class which represents an user, called "User" and an interface implemented by this class called "IUser". The IUser interface declares one method called "sendMail(String text)" for the user. Also i created an interceptor/ aspect via the Guice bindInterceptor method for my IUser. The aspect simply logs every call for the sendMail() method, that works fine.

1.) But how do i test, that my aspect works properly? I don't have an idea how to write an unit test for that. My idea was to create an IUser mock and trigger the public sendMail() method from outside. But how do I create an aspect on top of a mock?

First, I think you should unit test the interceptor itself, making sure that the log event is called. It's a little tricky, but you can achieve that with Mockito and log4j. 

If you really want to test the interceptor is binded correctly, you should write some kinda functional / integration test case. Here, you'll start your the Injector and check that the log is written or something alike.
 

2.) In my aspect/ interceptor i simply check the methodname like this: "if methodname == 'sendMail'". But if i rename/ refactor the sendMail Method my aspect won't work anymore. Is there a better way to to that?

You should use custom annotation for that and apply it only on the methods you want logging. Then configure your interceptor like:

bindInterceptor(
  any(), // classes
  annotatedWith(YourAnnon.class), // methods
  new YourLoggingInterceptor());

I think the examples about AOP in the wiki [1] are pretty clear, but let me know if I can help you further!

Cheers!
jordi

 

Thanks,
Dirk

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/6flNLUy1tfwJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply all
Reply to author
Forward
0 new messages