简体   繁体   English

使用Gradle AspectJ将生产方面编入测试类

[英]Weaving production aspect into test class with Gradle AspectJ

I am using the Gradle AspectJ plugin to weave some production aspect into test Java code. 我正在使用Gradle AspectJ插件将一些生产方面编织到测试Java代码中。 I would have expected this to work out of the box with the plugin, but apparently that's not the case as demoed here: https://github.com/sedubois/gradle-aspectj-poc/tree/dc44f529831a485fcff8f4889dba8098784dddb4 我原本预计这个插件开箱即用,但显然不是这里演示的情况: https//github.com/sedubois/gradle-aspectj-poc/tree/dc44f529831a485fcff8f4889dba8098784dddb4

The weaving of UnsupportedOperationAspect into MainSevice (both under src/main/java ) works, but the weaving of this same aspect into TestService (under src/test/java ) doesn't. UnsupportedOperationAspect编织到MainSevice (在src/main/java )可以正常工作,但是将这个相同的方面编织到TestService (在src/test/java )则不然。

I am new to Groovy, Gradle and AspectJ and didn't figure out if I should add some testAspectpath configuration or similar? 我是Groovy,Gradle和AspectJ的新手,并没有弄清楚我是否应该添加一些testAspectpath配置或类似的?

EDIT1: seems unrelated, but iajc gives a warning: EDIT1:似乎无关,但是iajc发出警告:

... :compileTestAspect [ant:iajc] [warning] incorrect classpath: [...]\\gradle-aspectj-poc\\build\\resources\\main ...

EDIT2: I naively added this code to the Gradle dependencies: EDIT2:我天真地将此代码添加到Gradle依赖项:

ajInpath fileTree(dir: "src/test/java")
aspectpath fileTree(dir: "src/test/java")
testAjInpath fileTree(dir: "src/test/java")
testAspectpath fileTree(dir: "src/test/java")

It doesn't help, the first test works and the second one fails as usual, with these new messages: 这没有帮助,第一次测试工作,第二次测试失败,这些新消息如下:

... :compileAspect [ant:iajc] [warning] build config error: skipping missing, empty or corrupt aspectpath entry: [...]\\gradle-aspectj-poc\\src\\test\\java\\com\\hello\\aop\\TestService.java [ant:iajc] [warning] build config error: skipping missing, empty or corrupt inpath entry: [...]\\gradle-aspectj-poc\\src\\test\\java\\com\\hello\\aop\\TestService.java ... :compileTestAspect [ant:iajc] [warning] build config error: skipping missing, empty or corrupt aspectpath entry: [...]\\gradle-aspectj-poc\\src\\test\\java\\com\\hello\\aop\\TestService.java [ant:iajc] [warning] build config error: skipping missing, empty or corrupt inpath entry: [...]\\gradle-aspectj-poc\\src\\test\\java\\com\\hello\\aop\\TestService.java [ant:iajc] [warning] incorrect classpath: [...]\\gradle-aspectj-poc\\build\\resources\\main ...

By default the plugin does not weave the main aspects in the test classes - we simply never made a configuration option for it. 默认情况下,插件不会在测试类中编织主要方面 - 我们根本就没有为它做过配置选项。 You can do this yourself using the following line: 您可以使用以下行自行完成此操作:

testAspectpath sourceSets.main.output

There is no expression matching TestService#serviceMethod() . 没有与TestService#serviceMethod()匹配的表达式。

In order to make your testcase work you need to advice your service method and (very important) the aspect must be located in the src/test/ package. 为了使您的测试用例工作,您需要建议您的服务方法,并且(非常重要)方面必须位于src/test/ package中。 Otherwise the compiler would not weave it in. 否则编译器不会编织它。

// located in `src/test/java` 

package com.hello.aop;

@Aspect
class UnsupportedOperationAspect {

    @Before("execution(void com.hello.aop.TestService.serviceMethod(..))")
    public void throwUnsupportedOperationOnMethod1() {
        throw new UnsupportedOperationException();
    }
}

Just a sidenote as i do not know what you are trying to achieve by advising test-classes (that are components specifically for test cases only): keep your tests as simple as possible. 只是旁注,因为我不知道你试图通过建议测试类(这是专门针对测试用例的组件)来实现的目标:尽可能简化测试。

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

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