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)

    IBusiness.java

    package com.spring.aop;

    public interface IBusiness {
    void doSomeOperation(int a, int b);
    }

    BusinessImpl.java

    package com.spring.aop;

    public class BusinessImpl implements IBusiness{
    int a;
    int b;

    public void doSomeOperation(int a, int b) {
    // TODO Auto-generated method stub
    this.a=a;
    this.b=b;
    System.out.print("a+b=");
    try {
    Thread.sleep(2000);
    }
    catch(InterruptedException e){
    System.out.println("Error");
    }
    System.out.println(a+b);
    }

    }

    BusinessProfiler.java

    package com.spring.aop;

    import org.aspectj.lang.ProceedingJoinPoint;

    public class BusinessProfiler {

    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    long start = System.currentTimeMillis();
    Object[] input={5,10};
    System.out.println("Going to call the method.");
    for ( Object object : pjp.getArgs()) {
    System.out.println("--->"+object);
    }
    Object output = pjp.proceed(input);
    System.out.println("Method execution completed.");
    long elapsedTime = System.currentTimeMillis() - start;
    System.out.println("Method execution time: " + elapsedTime + " milliseconds.");
    //System.out.println(output);
    return output;
    }
    }

    SpringAop.java

    package com.spring.aop;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class SpringAop {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
    IBusiness bc = (IBusiness) context.getBean("myBusinessClass");
    bc.doSomeOperation(10,20);
    }

    }

    config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schem...ng-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />
    <bean id="myBusinessClass" class="com.spring.aop.BusinessImpl" />
    <!-- @Aspect -->
    <bean id="businessProfiler" class="com.spring.aop.BusinessProfiler" />
    <aop:config>
    <aop:aspect id="aspectLoggging" ref="businessProfiler" >
    <!-- @Around -->
    <aopointcut id="pointCutAround"
    expression="execution(* com.spring.aop.BusinessImpl.doSomeOperation(..))" />
    <aop:around method="profile" pointcut-ref="pointCutAround" />
    </aop:aspect>
    </aop:config>
    </beans>

    These are my required files. I want to know how to unit test "profiler" method in "BusinessProfiler.class" using JUnit4.
    Please post me the solution.

    Thanks & Regards,
    Kannan
Working...
X