spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.17k stars 40.68k forks source link

Cannot use Gradle to test spring-boot code in separate test module, throws NoClassDefFoundError #34502

Closed zhangvv123 closed 1 year ago

zhangvv123 commented 1 year ago

I created a project

- frog
  -- frog-web
  -- frog-test

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

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
```java
@SpringBootApplication
public class FrogWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(FrogWebApplication.class, args);
    }
}

frog-test

test { useJUnitPlatform() }

wilkinsona commented 1 year ago

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.