简体   繁体   English

如何避免在测试期间调用 spring AOP 方面

[英]How to avoid spring AOP aspect being called during test

I need to avoid an aspect being called when unit testing a class.我需要避免在对 class 进行单元测试时调用某个方面。

I'm working with Java 8, spring 4.3.22.RELEASE and mockito. I have a @Service and a unit test for it.我正在使用 Java 8、spring 4.3.22.RELEASE 和 mockito。我有一个 @Service 和一个单元测试。 I also have an @Aspect that defines a pointcut on a method in the service and it is working fine when I run my application.我还有一个 @Aspect 定义了服务中方法的切入点,当我运行我的应用程序时它工作正常。 The problem is when I run my unit test, the aspect is called and a NullPointerException is raised because of a missing dependency in the aspect.问题是当我运行我的单元测试时,方面被调用并且由于方面中缺少依赖项而引发 NullPointerException。

Service class:客服class:

@Service
public class ContactService {

    @Autowired
    public InContactService(ContactDao contactDao) {
        this.contactDao = contactDao;
    }

    public boolean muteCall(Long contactId) {
        return contactDao.muteCall(contactId);
    }
}

Service test:服务测试:

public class ContactServiceTest {

  @Mock
  private ContactDao contactDao;

  private ContactService contactService;


  @Before
  public void setUp(){
    MockitoAnnotations.initMocks(this);
    contactService = new ContactService(contactDao);
  }

  @Test
  public void testMuteCall(){
    contactService.muteCall(1L);
  }
}

Aspect:方面:


@Aspect
public class ContactAspect {

    private MeterRegistry registry;

    public void setRegistry(MeterRegistry registry) {
        this.registry = registry;
    }

    @AfterReturning(pointcut = "execution(* com.company.ContactService.muteCall(..))", returning = "retVal")
    public void checkReturnContactServiceMuteCall(JoinPoint joinPoint, boolean retVal) {
        Object[] args = joinPoint.getArgs();

      registry.counter("my.metric.mute_call").increment();
    }
}

Application context:应用上下文:

@Configuration
public class ApplicationContext {

    @Bean
    public MeterRegistry meterRegistry() {
      return new SimpleMeterRegistry();
    }

    @Bean
    public ContactAspect contactAspect() {
        ContactAspect aspect =   Aspects.aspectOf(ContactAspect.class);
        aspect.setRegistry(meterRegistry());
        return aspect;
    }
}

I expected that when the test is ran the aspect is not called.我预计在运行测试时不会调用方面。 Currently I get a NullPointerException when I run the test because registry is not defined in the aspect.目前,我在运行测试时收到 NullPointerException,因为registry未在方面中定义。

The best approach is using Spring profiles, which allows you to have different running schemes.最好的方法是使用 Spring 配置文件,它允许您有不同的运行方案。

check this: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html检查这个: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

I ran into this issue with legacy code to which I wanted to add integration tests but didn't need or want the aspects to be invoked.我在遗留代码中遇到了这个问题,我想向其中添加集成测试,但不需要或不希望调用这些方面。

There most likely is somewhere in your context configuration telling the application to enable aspects.很可能在您的上下文配置中的某个地方告诉应用程序启用方面。 Wherever that is, find it, and disable it.无论在哪里,找到它并禁用它。

In my case, the configs were XML based so in my applicationContext-services-integration-test.xml file being loaded for my integration tests, I commented out <aop:aspectj-autoproxy /> and it bypassed all the aspects for my tests.在我的例子中,配置是基于 XML 的,所以在我的applicationContext-services-integration-test.xml文件为我的集成测试加载时,我注释掉了<aop:aspectj-autoproxy />并绕过了我的测试的所有方面。

Cheers!干杯!

We've run into the same problem and fixed it by disabling property when running tests.我们遇到了同样的问题,并通过在运行测试时禁用属性来修复它。

import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

@Aspect
@ConditionalOnExpression("${aspect.property.enabled:true}")
public class AspectClass {

test/resources/application.properties测试/资源/application.properties

aspect.property.enabled=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM