【发布时间】:2019-11-24 01:41:57
【问题描述】:
我正在尝试将现有的 Java 8 多项目 gradle 应用程序升级到 Java 11。在修复了一些编译问题后,我最终在测试用例中遇到了问题。当我在 Intellij 中运行测试时,它会引发以下错误:
错误:java:尝试重新创建类型为 {QueryDsl 类}的文件
它正在尝试生成 Querydsl 类,但由于这些类已经存在,因此测试会抛出异常。
我正在使用 Java11、IntelliJ 2019、Gradle 5 来运行应用程序。
这些测试在 Java8 中按预期工作。
我不知道是什么导致了这个错误。任何人都可以帮助我理解这一点。
代码sn-ps如下。
根项目 build.gradle:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE)
classpath("net.ltgt.gradle:gradle-apt-plugin:0.21")
}
}
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
task allDependencies(type: DependencyReportTask) {}
jar {
baseName = "${parent.name}-${project.name}"
}
sourceSets {
main {
java {
srcDirs 'src/main/java', 'build/generated/sources/main/java', 'build/generated/sources/annotationProcessor/java/main'
}
}
}
}
子项目build.gradle:
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'net.ltgt.apt'
bootJar {
baseName = "test"
version = "1.0.0"
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa: 2.1.4.RELEASE")
compile("com.querydsl:querydsl-core:4.1.3")
compile("com.querydsl:querydsl-jpa:4.1.3")
annotationProcessor(
"com.querydsl:querydsl-apt:4.1.3:jpa",
"javax.annotation:javax.annotation-api:1.3.2"
)
annotationProcessor("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
testCompile("org.springframework.boot:spring-boot-starter-test:2.1.4.RELEASE")
testCompile("com.h2database:h2:2.1.4.RELEASE")
}
集成测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainClass.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ServiceImplTest {
@Autowired
private Service1Impl service;
@Test
public void getData() {
Data data = service.getData();
Assert.assertEquals(0, data.size());
}
}
【问题讨论】:
-
请出示您的代码。
-
@Codo 我已经添加了必要的代码 sn-ps。
标签: spring-boot gradle querydsl java-11 spring-boot-test