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()
}

Comment From: wilkinsona

Spring Boot 2.3 has been out of support for almost three years. Please upgrade to Spring Boot 2.7.x or later. If the problem remains, you will need to configure your frog-test project to consume the plain jar that frog-web builds with its jar task rather than the fat jar that it builds with its bootJar task. If you have any further questions about this, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.