Announcement Announcement Module
Collapse
No announcement yet.
How to unit test aop(aspect class) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to unit test aop(aspect class)

    How to unit test the following method using JUnit4??

    public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
    Object serviceObject = null;

    BaseServiceRequest baseServiceRequest = null;

    ServiceContext serviceContext = null;

    baseServiceRequest = (BaseServiceRequest) joinPoint.getArgs()[0];

    if (baseServiceRequest == null)
    ExceptionHandlerUtil.createExceptionType("SL",
    "BaseServiceRequest cannot be empty", new Object[] {});

    serviceContext = baseServiceRequest.getServiceContext();

    ExceptionHandlerUtil.createExceptionType("SL",
    "ServiceContext cannot be empty", new Object[] {});

    createServiceInvocationInterceptor(baseServiceRequ est,joinPoint);

    serviceObject = getServiceObject(serviceContext, joinPoint);

    serviceContext.getMethodInvocationEvent().setRetur nValue(serviceObject);

    return serviceObject;

    }
    Last edited by DranzeR; Jun 20th, 2012, 11:45 AM.

  • #2
    Well...just as you would do for any other method of any other class...after all, an aspect class IS a java class and an aspect method IS a Java method!

    The boring part will be mocking the pjp object, you can use on of the several mocking frameworks out there (Mockito, EasyMock etc.) to do that. Other than that, it's just a matter of invoking the method and asserting your results.

    Comment


    • #3
      Thanx Enirico, I am passing proceeding join point as an argument to the method invoke. For junit testing i need to declare an object for proceeding join point in the method setUp(). The problem is i cant instantiate an object for proceeding join point because it is an interface. Any alternate solution for this??

      Comment


      • #4
        I already answered to this question:

        The boring part will be mocking the pjp object, you can use on of the several mocking frameworks out there (Mockito, EasyMock etc.) to do that.
        You probably never used a mocking framework, what they do is you give them an interface and they instantiate for you a default implementation of that interface that you can pass to the method call. You can of course set up and drive fie-grained behavior of the mocked object, just google "mocking frameworks", "Mockito" or "Easy Mock".

        Comment

        Working...
        X