spring - gradle-aspectj:编织和ajc编译器选项在编译时起作用,但在测试任务中不起作用

标签 spring gradle aspectj build.gradle ajdt

我已经通过AJDT插件在Eclipse中成功建立了编织,并通过gradle-aspectj插件在gradle构建中成功建立了编织(这花了很多时间...)。

在Eclipse中,这既适用于生产代码也适用于测试代码,即所有测试均通过。
当我运行gradle构建时,生成的应用程序也可以正常运行,并且可以看到各个方面都按预期工作。

gradle“测试”任务失败,因为许多测试失败。我可以将大多数故障追溯到某些方面(在这里:针对 Spring 交易)不起作用,或者某些ajc-compiler选项用于编码无效(有关详细信息,请参见here)。从Eclipse触发时,相同的测试运行良好。

为了使编织也能进行测试,是否还需要一些其他配置?

我找到了一些相关的question,但这并不能为我解决问题。看起来仍然没有考虑到方面,也没有激活编译器选项(我仅在测试中看到编码错误)。

我的(缩写)build.gradle(注意:我很难完全开始工作,因此其中可能包含一些不必要的配置):

buildscript
{
  dependencies
  {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    classpath("nl.eveoh:gradle-aspectj:1.6")
  }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'aspectj'

configurations {
  runtime
  testCompile {
    extendsFrom testAspectpath
  }
  aspectpath
  testAspectpath
  ajInpath
  compile {
    extendsFrom aspectpath
  }
}

dependencies
{
  // Non aspect dependencies
  // ...

  // Dependencies that require weaving - works for compile but not for test task
  aspectpath("org.springframework:spring-context:4.2.1.RELEASE")
  compile("org.springframework:spring-context:4.2.1.RELEASE")
  aspectpath("org.springframework:spring-context-support:4.2.1.RELEASE")
  compile("org.springframework:spring-context-support:4.2.1.RELEASE")
  compile 'com.vaadin:vaadin-spring-boot-starter:1.0.0'
  testCompile("org.springframework.boot:spring-boot-starter-test:${springVersion}")

  // Spring Data Neo4j
  compile "org.springframework.data:spring-data-neo4j:${springDataGraphVersion}"

  // Additional aspects - also need to be configured in ADJT
  aspectpath "org.aspectj:aspectjtools:${aspectjVersion}"
  compile "org.aspectj:aspectjrt:${aspectjVersion}"
  testCompile "org.aspectj:aspectjrt:${aspectjVersion}"
  compile "org.springframework:spring-aspects:4.2.1.RELEASE"
  aspectpath "org.springframework:spring-aspects:4.2.1.RELEASE"
  compile "org.springframework:spring-instrument:3.2.1.RELEASE"
  aspectpath "org.springframework:spring-instrument:3.2.1.RELEASE"
  compile "org.springframework.data:spring-data-neo4j-aspects:${springDataGraphVersion}"
  aspectpath "org.springframework.data:spring-data-neo4j-aspects:${springDataGraphVersion}"
  // Required by spring aspects
  compile "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
  aspectpath "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
  compile "javax.persistence:persistence-api:1.0"
  aspectpath "javax.persistence:persistence-api:1.0"
  testAspectpath sourceSets.main.output // from related question [4]
}

compileAspect {
  // Works in compile but not in test task!
  additionalAjcArgs = ['encoding' : 'UTF-8']
}

编辑:编码问题已通过kriegaex的代码段解决。
从gradle运行时,测试中不存在的方面问题仍然存在。
大多数测试由于以下原因而失败

org.neo4j.graphdb.NotInTransactionException



表示基于方法的注释

@Transactional



是无效的。
有什么想法吗?

最佳答案

我没有尝试过,但是根据plugin description,我认为您应该将其添加到Gradle配置文件中:

compileTestAspect {
  additionalAjcArgs = ['encoding' : 'UTF-8']
}

关于spring - gradle-aspectj:编织和ajc编译器选项在编译时起作用,但在测试任务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474273/

相关文章:

java - gradle:未定义存储库

android - 在Android Studio中将 `MissingTranslation`升级为错误

java - 如何使用单独的 jar 覆盖类?

java - 未调用 JdbcOperations 中使用的 Spring AOP 方面

java - 使用 Spring AspectJ 记录 REST 资源 URL

java - Spring 测试因 NoClassDefFoundError 失败

java - Spring 5 Security 支持 $2y$ bcrypt 哈希版本吗?

java - 如何使用 Geoserver 部署现有的 REST 服务

java - 如何在 Spring Controller 中模拟用户,并在某些内容丢失或未模拟时进行调试?

java - 创建了一个自定义 Spring boot layoutFactory,现在无法启动我的 Spring Boot 应用程序