Note that there are some explanatory texts on larger screens.

plurals
  1. POAspectJ advice not being executed through unit test
    primarykey
    data
    text
    <p>I'm stumped. I'm trying to test an AspectJ class. My Aspect class gets picked up perfectly when I'm running my application. However, I seem to be unable to get any Aspect class to intercept any method within a test.</p> <p>I am using Spring 3.2.2, AspectJ 1.7.2 and Maven 4. </p> <p>Here is the simple test I'm working with:</p> <p>The Test AspectJ class</p> <pre><code>package my.package.path.config; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Aspect public class TestAOP { private String message; public TestAOP() { } @Pointcut("execution(* my.package.path.TestAOPClient.relayMessage(..))") public void aopPointcut() { } @Around("aopPointcut()") public String monitor(ProceedingJoinPoint pjp) throws Throwable { String msg = (String)pjp.proceed(); this.setMessage(msg); return msg; } </code></pre> <p>}</p> <p>The class whose method is being intercepted</p> <pre><code>package my.package.path.config; public class TestAOPClient { public String relayMessage(String msg) { return msg; } } </code></pre> <p>The Test Class</p> <pre><code>package my.package.path.config; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @Configuration @ContextConfiguration(classes={WebConfig.class}) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("src/main/java") public class AopConfigTest extends AbstractJUnit4SpringContextTests { @Bean public TestAOP testAop() throws Exception { return new TestAOP(); } @Test public void assertTestConfigIsActive() { TestAOPClient client = new TestAOPClient(); client.relayMessage("hello"); assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(), equalTo("hello")); } } </code></pre> <p>The WebConfig file</p> <pre><code>package my.package.path.web.context; @Configuration @EnableWebMvc @EnableAspectJAutoProxy(proxyTargetClass=false) @ComponentScan(value={"my.package.path.config", "my.package.path.web"}) public class WebConfig { } </code></pre> <p>Invariably, I will get the assertion error </p> <pre><code> Expected: "hello" but: was null </code></pre> <p>My WebApplicationContext seems to be picked up, since at runtime, I will get an ApplicationContext failed to load error if I specify a class that does not exist for my Aspect pointcut.</p> <p>What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload