Java – no AspectJ recommendations executed through unit tests

I'm sorry. I'm trying to test an AspectJ class When I run my application, my aspect class is picked up perfectly However, I can't seem to let any aspect class intercept any method in the test

I use spring 3.2 2,AspectJ 1.7. 2 and Maven 4

This is the simple test I'm using:

Test AspectJ class

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;
    }

}

The class whose methods are being intercepted

package my.package.path.config;

public class TestAOPClient {
    public String relayMessage(String msg) {
        return msg;
    }
}

Test class

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"));

    }
}

Webconfig file

package my.package.path.web.context;
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=false)
@ComponentScan(value={"my.package.path.config","my.package.path.web"})
public class WebConfig {

}

Always, I get assertion errors

Expected: "hello" but: was null

My webapplicationcontext seems to be selected because at runtime, if I specify a class that does not have my aspect pointcut, I will get an ApplicationContext unable to load error

What did I miss?

Solution

It's a little strange that you also use unit tests as @ configuration source

You should remove the @ configuration annotation from the unit test and move the testaop () bean definition to webconfig But most importantly, the recommended bean cannot be created manually, but by spring:

@ContextConfiguration(classes={WebConfig.class})
@WebAppConfiguration("src/main/java")
public class AopConfigTest extends AbstractJUnit4SpringContextTests {

     @Autowired
     private TestAOP testAop;

     @Autowired
     private TestAOPClient client;

     @Test
     public void assertTestConfigIsActive() {
        client.relayMessage("hello");
        assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(),equalTo("hello"));
    }

}

Updated configuration with bean definition:

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=false)
@ComponentScan(value={"my.package.path.config","my.package.path.web"})
public class WebConfig {

    @Bean
    public TestAOP testAop() throws Exception {
        return new TestAOP();
    }

    @Bean
    public TestAOPClient testAopClient() throws Exception {
        return new TestAOPClient();
    }

}

If your goal is to test whether the AOP configuration is normal and testaop is indeed a test bean (not just the virtual name of this problem), you can create a special testconfig configuration class, move the bean definition there and use its contextconfiguration (class = {webconfig. Class, testconfig. Class}) from the test @

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>