开发者

Ignoring Aspectj during junit tests

Here is situation:

  1. We have class with defined aspect to it's methodA;
  2. We have JUnit test for this methodA;

When I run JUnit 开发者_运维技巧test it activates Aspect as well. Any thoughts how to ignore Aspects during unit tests?

I have separated tests for my Aspects and it works fine. So in my unit test I want to test only methodA without any attached aspects.

I use spring 3.0 and its aspectj support.

Thanks in advance.

Regards, Max


You can disable the compile-time weaving that I assume your IDE is doing and use load-time weaving in your separated AspectJ tests.

To enable load-time weaving you have to provide a javaagent as an JVM parameter.

An example:

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

Other changes when you move from compile-time to load-time weaving

You must also provide an aop.xml file in the META-INF folder on the 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 can see that the TraceAspect class will be weaved with all the classes in the demo package.

Spring configuration with load-time weaving

<?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 it also contains a load-time weaver element.

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜