-
Jun 20th, 2012, 11:37 PM
#1
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 -->
<aop
ointcut 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
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
-
Forum Rules