Ignoring Aspectj during junit tests

Here is the situation:

  • We have a class with a specific aspect for method A;
  • We have a JUnit test for this method A;

When I run the JUnit test, it also activates Aspect. Any thoughts on how to ignore aspects during unit tests?

I have split tests for my aspects and it works great. Therefore, in my unit test, I want to test only Method A without any attached aspects.

I am using spring 3.0 and its aspectj support.

Thanks in advance.

Regards, Max

+6
source share
1 answer

You can turn off compile-time weaving, which I believe your IDE runs, and use load-time markup in your individual AspectJ tests.

To enable weaving at boot time, you need to provide javaagent as a JVM parameter.

Example:

-javaagent:lib/spring-dependencies/spring-agent.jar 

Other changes when switching from compile time to triangulation at boot time

You must also provide the aop.xml file in the META-INF folder on claspath. For my trace example, it looks like this:

 <!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> <aspectj> <weaver> <!-- only weave classes in this package --> <include within="aspects.trace.demo.*" /> </weaver> <aspects> <!-- use only this aspect for weaving --> <aspect name="aspects.trace.TraceAspect" /> </aspects> </aspectj> 

In this configuration, you will see that the TraceAspect class will be woven with all classes in the demo package.

Spring boot-time spinning configuration

 <?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="traceAspect" class="aspects.trace.TraceAspect" factory-method="aspectOf"/> <context:load-time-weaver /> </beans> 

The configuration file is almost the same as the compile-time configuration file, except that it also contains a load-time weaving element.

Hope this helps!

+4
source

Source: https://habr.com/ru/post/898130/


All Articles