我创建了一个项目
- frog
-- frog-web
-- frog-test
- frog是根模块,没有代码
- frog-web是一个Spring引导模块,配置了bootJar,将封装成一个完整的jar
- frog-test是引用frog-web的单元测试模块,在测试frog-web类时抛出java. lang. NoClassDefFoundError
site/weic/grog/web/FrogWebApplication
java.lang.NoClassDefFoundError: site/weic/grog/web/FrogWebApplication
at site.weic.frog.test.A01Test.a(A01Test.java:16)
...
我应该如何配置它,以便我可以在frog-test模块中对frog-web的类进行单元测试?谢谢!!!
这是密码
青蛙
- build.gradle
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.12.RELEASE")
}
}
allprojects {
apply plugin: 'java-library'
apply plugin: 'org.springframework.boot'
group 'site.weic'
version 'dev-v0.2-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
tasks.withType(JavaCompile) {
options.encoding 'UTF-8'
}
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public' }
mavenCentral()
}
bootJar {
enabled false
}
}
subprojects {
apply plugin: 'io.spring.dependency-management'
dependencies {
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
implementation 'org.slf4j:slf4j-api'
}
dependencyManagement {
dependencies {
dependency "org.projectlombok:lombok:1.18.26"
dependency "mysql:mysql-connector-java:8.0.23"
}
imports {
mavenBom "org.springframework.boot:spring-boot-starter-parent:2.3.12.RELEASE"
}
}
}
蛙蹼
- site.weic.grog.web.FrogWebApplication
@SpringBootApplication
public class FrogWebApplication {
public static void main(String[] args) {
SpringApplication.run(FrogWebApplication.class, args);
}
}
- build.gradle
bootJar {
enabled true
}
dependencies {
api 'org.springframework.boot:spring-boot-starter-web'
}
蛙跳测试
- site.weic.frog.test.A01Test
public class A01Test {
@Test
public void a(){
// Here to test whether frog-web's classes can be access in module frog-test,
// NoClassDefFoundError will be thrown
final FrogWebApplication web = new FrogWebApplication();
System.out.println(123);
}
}
- build.gradle
dependencies {
implementation project(":frog-web")
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
1条答案
按热度按时间zpqajqem1#
Sping Boot 创建了一个结构稍有不同的fat/可执行jar(请参见Spring Boot参考指南),由于这种差异,您不能将此jar用作其他项目的依赖项。
Sping Boot 参考指南中也解释了如何创建可用作依赖项的附加构件。
然而奇怪的是你把你的测试从实际的工件中分离出来,如果它们是集成测试,使用你的测试脚本来启动应用程序,发出http请求,并验证这些请求,不要直接引用类。