I created a project
- frog
-- frog-web
-- frog-test
- frog is the root module, without code
- frog-web is a spring-boot module, configured with bootJar, will be packaged into a whole jar
- frog-test is a unit test module that references frog-web and throws java.lang.NoClassDefFoundError when testing frog-web classes
site/weic/grog/web/FrogWebApplication
java.lang.NoClassDefFoundError: site/weic/grog/web/FrogWebApplication
at site.weic.frog.test.A01Test.a(A01Test.java:16)
...
How should I configure it so that I can unit test the frog-web's classes in the frog-test module? THANKS!!!
This the code
frog
- 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"
}
}
}
frog-web
- 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'
}
frog-test
- 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()
}