[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] Using AspectJ and mock objects
|
Hi, I'm trying to use aspects to mock 'on the fly' a domain layer class. I've tried to follow Nicholas Leseicki's great article at http://www-106.ibm.com/developmerworks/java/library/j-aspect2. I've had some luck, but I'm getting some unexpected behavior.
My aspect looks like this - real names replaced to protect the innocent.
import java.util.Collection;
// import other stuff
public aspect DAOAspect
{
pointcut inTest() : execution(public void MyTestCase.*());
pointcut findThings(String searchStr) :
cflow(inTest()) &&
call(Collection MyDAO.find*(*)) && // MyDAO is a concrete class, not an interface.
!call(Collection MockMyDAO.find*(*)) &&
args(searchStr);
Collection around(String searchStr) : findThings(searchStr)
{
System.out.println("In the aspect");
System.out.println("searchStr is : " + searchStr);
return new MockMyDAO().findStuff(searchStr);
}
}
First bit of unexpected behavior:
I see that my aspect is called twice via the System.out.println()s
"In the aspect"
"search"
"In the aspect"
"search"
...
Why would this happen? I believe I'm only calling into the mocked DAO once.
// test case
public void testFind()
{
assertTrue(0 < daoCollaborator.find("search"));
}
// code under test in DAOCollaborator
public Collection find(String searchStr)
{
return new MyDAO().findStuff(searchStr);
}
// MyMockDAO over-ride
public Collection findStuff(String searchStr)
{
return mockStuff;
}
Second question/behavior.
Is it possible for me to just return the MockDAO whenever the DAO is invoked/used? It would make it easier for me to just return the mock object reference instead of having the aspect return method level items. I'm not clear on the syntax/advice I need to use to do this.
thanks,
---
Keith Sader
Nash Resources Group sub-contracting for Computer Sciences Corporation
IAD:TFAS
Email:keith.d.sader@xxxxxxxx
We've got a blind date with destiny, and it looks like she ordered the lobster.