Hi Guys,
AOP newbie;
I hope someone can help me I'm developing a quick AOP proof of concept with the
intention to roll it out company wide. The idea is to use AOP to mock-out dependencies in
order to simplify unit testing. I'm sure I don't have to state the benefits here.
However I have created a basic J2EE project with a single SessionBean and a few entities.
I have dependencies created in the constructor of the SessionBean which I want to mock
out.
private IPersistenceHelper helper;
| public AOPUnitTestManagerBean() {
| helper = new PersistenceHelper(PERSISTENCE_CONTEXT_NAME);
| }
I have written an simple Interceptor which intercepts the PersistenceHelper constructor
and replaces it with a MockPersistenceHelper;
public class PersistenceHelperInterceptor implements Interceptor {
|
| /**
| * Log4j Logger for this class
| */
| private static final Logger log = Logger
| .getLogger(PersistenceHelperInterceptor.class);
|
| public String getName() {
| return "PersistenceHelperInterceptor";
| }
|
| public Object invoke(Invocation invocation) throws Throwable {
| if (log.isDebugEnabled()) {
| log.debug("inside invoke(arg0)");
| }
| Object helper = new MockPersistenceHelper(null);
| return helper;
| }
| }
I can get this working if the MockPersistenceHelper extends the PersistenceHelper. This is
not my preferred solution since it would be better if the MockPersistenceHelper
implemented the same interface as the PersistenceHelper, this ensures that all methods
have a mock implementations as oppsoed to simply over-riding the methods (open to abuse).
the jboss-aop.xml to get this to work;
<?xml version="1.0" encoding="UTF-8"?>
| <aop>
| <bind pointcut="execution(public
com.xxx.unittest.aop.PersistenceHelper->new(java.lang.String))">
| <interceptor
class="com.xxx.unittest.aop.interceptor.PersistenceHelperInterceptor"/>
| </bind>
| </aop>
So I have changed it to something like;
<?xml version="1.0" encoding="UTF-8"?>
| <aop>
|
| <bind pointcut="execution(public
$typedef{persistenceHelperTypeDef}->new(java.lang.String))">
| <interceptor
class="com.xxx.unittest.aop.interceptor.PersistenceHelperInterceptor"/>
| </bind>
|
| <typedef name="persistenceHelperTypeDef"
| expr="class($instanceof{com.xxx.unittest.aop.IPersistenceHelper})
| AND !class($instanceof{com.xxx.unittest.aop.mock.*})" />
| </aop>
However every option I have tries either gives me a ClassCastException, returned object
not as expected, or a Stack overflow since the MockPersistenceHelper is itself intercepted
since it implements the defined interface.
So I suppose the question is how can I define the typedef such that;
1. It intercepts the constructor of classes of IPersistenceHelper but does not intercept
those classes in the xx.xx.xx.mock package.
Hope that wasn't too long winded to ask, hopefully, a straightforward question. This
all occurs outside the JBossAS container!
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4215355#...
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&a...