Announcement Announcement Module
Collapse
No announcement yet.
Trying to test basilar AOP but advices are never called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    Code:
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(testxml));
    A BeanFactory is only a factory. Read chapter
    3.1
    of the reference guide, especially the outset.

    In short always use an ApplicationContext.

    Code:
    ApplicationContext factory = new FileSystemXmlApplicationContext(testxml);

    Comment


    • #3
      Thank you very much Marten: you were right and now everything works fine!

      Sorry for the trivial question but I'm very new to the spring-world and there is a lot of stuff to pay attention to!

      Comment

      Working...
      X