Results 1 to 4 of 4

Thread: How to unit test aop(aspect class)

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Default 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 at 11:45 AM.

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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.

  3. #3
    Join Date
    Jun 2012
    Posts
    3

    Default

    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??

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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".

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •