Trying to test basilar AOP but advices are never called
Hello,
I'm a spring newbie and I'm trying to practise myself into AOP, so sorry for the very basilar question. I'm reading Spring in Action and I'm trying to set-up some tests but I'm not having the results I expect.
I have this class:
Code:
import biz.elabor.roundtable.quests.Quest;
public class KnightOfTheRoundTable implements Knight {
...
public Object embarkOnQuest() throws QuestException {
return this.quest.embark();
}
And this other class:
Code:
package biz.elabor.roundtable;
public class Minstrel {
public void singBefore(Knight knight) {
System.out.println("sing before actions");
}
public void singAfter(Knight knight) {
System.out.println("sing after actions");
}
And I'm trying to use the Minstrel in order to sing before and after the Knight embark for his quest.
So I've this test-knight.xml file:
Code:
<?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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="quest" class="biz.elabor.roundtable.quests.HolyGrailQuest" />
<bean id="knight"
class="biz.elabor.roundtable.knights.KnightOfTheRoundTable">
<constructor-arg value="Bedivere" />
<property name="quest" ref="quest" />
</bean>
<bean id="minstrel" class="biz.elabor.roundtable.Minstrel" />
<aop:config>
<aop:aspect id="singQuest" ref="minstrel">
<aop:pointcut id="questPointcut"
expression="execution(* *.embarkOnQuest(..)) and target(knight)" />
<aop:before pointcut-ref="questPointcut"
method="singBefore"
arg-names="knight" />
<aop:after-returning method="singAfter"
pointcut-ref="questPointcut"
arg-names="knight" />
</aop:aspect>
</aop:config>
</beans>
And I tried to test all this stuff with this unit test:
Code:
public class KnightTestCase extends TestCase {
private Knight knight;
private Minstrel minstrel;
public void setUp() {
String testxml = "test/test-knight.xml";
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(testxml));
this.knight = (Knight) factory.getBean("knight");
this.minstrel = (Minstrel) factory.getBean("minstrel");
assertNotNull(this.knight);
assertNotNull(this.minstrel);
}
public void testEmbarkOnQuest() {
try {
HolyGrail grail = (HolyGrail) this.knight.embarkOnQuest();
assertNotNull(grail);
assertTrue(grail.isHoly());
} catch (QuestException e) {
fail();
}
}
}
The test runs (green bar) but the minstrel methods are never called (I don't see any message in the console). I don't understand what I'm missing.
Any help?
Thanks in advance, :)
Ivan