集成测试中的Springboottest classNotFoundException错误

发布于 2025-01-22 18:56:20 字数 4815 浏览 0 评论 0 原文

我一直在与Springbootest挣扎了几天。我最初发布了这个问题。我将其减少到了一个简单得多的情况。

我有一个非常简单的应用程序,具有单个服务和一个应用程序类。 服务类:

package com.sodved.itestcase0.app.service;

import org.springframework.stereotype.Service;

@Service
public class AppService {

    public String getServiceName() {
        return this.getClass().getCanonicalName();
    }
}

应用程序:

package com.sodved.itestcase0.app;

imports...

@Slf4j
@EnableScheduling
@EnableAsync(proxyTargetClass=true)
@SpringBootApplication(scanBasePackages={"com.sodved.itestcase0"})
public class Case0Application implements CommandLineRunner {

    @Autowired
    private AppService appService;

    public static void main(String[] args) {
        log.info("STARTING THE APPLICATION");
        ConfigurableApplicationContext context = SpringApplication.run(Case0Application.class, args);
        log.info("APPLICATION FINISHED");
        context.close();
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Application running...");
        log.info("App Service: {}", appService.getServiceName());
    }

}

所有运行良好:

spring-itest> java -jar case0/case0-app/target/case0-app-0.0.1-0-SNAPSHOT.jar
...
2022-04-21 12:23:53.589  INFO 65095 --- [           main] c.s.itestcase0.app.Case0Application      : Application running...
2022-04-21 12:23:53.589  INFO 65095 --- [           main] c.s.itestcase0.app.Case0Application      : App Service: com.sodved.itestcase0.app.service.AppService

但是我的集成测试与 classNotFoundException 失败。

集成测试上下文(如果我使用应用程序类本身,没有运行测试???):

package com.sodved.itestcase0.app.itest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication(scanBasePackages={"com.sodved.itestcase0"})
public class DummyApplicationContext {

    public static void main(String[] args) {
        log.info("Dummy Context (Not sure if this should ever run)");
    }

}

集成测试类:

package com.sodved.itestcase0.app.itest;

import com.sodved.itestcase0.app.service.AppService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes=DummyApplicationContext.class)
public class IntegrationIT {

    @Autowired
    private AppService appService;

    @Test
    public void testApplicationLoaded() {
        log.info("Application loaded OK");
    }

    @Test
    public void testAppSerivce() {
        log.info("App Service: {}", appService.getServiceName());
    }

}

测试失败:

spring-itest> mvn clean verify -P itest -pl case0 -am -amd
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sodved.itestcase0.app.itest.IntegrationIT
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.sodved.itestcase0.app.itest.IntegrationIT
[ERROR] com.sodved.itestcase0.app.itest.IntegrationIT  Time elapsed: 0.002 s  <<< ERROR!
java.lang.NoClassDefFoundError: Lcom/sodved/itestcase0/app/service/AppService;
Caused by: java.lang.ClassNotFoundException: com.sodved.itestcase0.app.service.AppService

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   IntegrationIT » NoClassDefFound Lcom/sodved/itestcase0/app/service/AppService;
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

所以我认为这是我简单的弹药(我的pom.xml中有问题?)。 我指出的其他一些点:

  • 仅针对正在测试的模块中定义的组件类问题。如果组件类来自依赖项,一切都可以正常工作(请参阅“ nofollow noreferrer”> case1 in Github )。
  • 如上所述,如果我使用 case0application 作为Springboottest类,则不会运行测试。这就是为什么我创建虚拟上下文的原因。您可以在

我的示例源代码可在此处找到: https://github.com/sodved/spring/spring-itest-empest-itest-ept- >

我一直在猛击这个几天。读取教程和手册等的堆。感觉就像是测试配置/类Path的问题,但不确定它是我的错误所在的Maven还是Spring引导。我无法弄清楚。

我的真正问题涉及数据库容器等,但这是我可以复制问题的最简单情况。任何帮助将不胜感激。

I have been struggling with SpringBootTest for several days now. I initially posted this question. I have reduced it to a much simpler case.

I have a very simple app with a single service and an application class.
Service class:

package com.sodved.itestcase0.app.service;

import org.springframework.stereotype.Service;

@Service
public class AppService {

    public String getServiceName() {
        return this.getClass().getCanonicalName();
    }
}

Application:

package com.sodved.itestcase0.app;

imports...

@Slf4j
@EnableScheduling
@EnableAsync(proxyTargetClass=true)
@SpringBootApplication(scanBasePackages={"com.sodved.itestcase0"})
public class Case0Application implements CommandLineRunner {

    @Autowired
    private AppService appService;

    public static void main(String[] args) {
        log.info("STARTING THE APPLICATION");
        ConfigurableApplicationContext context = SpringApplication.run(Case0Application.class, args);
        log.info("APPLICATION FINISHED");
        context.close();
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Application running...");
        log.info("App Service: {}", appService.getServiceName());
    }

}

That all runs fine:

spring-itest> java -jar case0/case0-app/target/case0-app-0.0.1-0-SNAPSHOT.jar
...
2022-04-21 12:23:53.589  INFO 65095 --- [           main] c.s.itestcase0.app.Case0Application      : Application running...
2022-04-21 12:23:53.589  INFO 65095 --- [           main] c.s.itestcase0.app.Case0Application      : App Service: com.sodved.itestcase0.app.service.AppService

But my integration test fail with a ClassNotFoundException.

Integration test context (if I use the Application class itself, no tests are run???):

package com.sodved.itestcase0.app.itest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication(scanBasePackages={"com.sodved.itestcase0"})
public class DummyApplicationContext {

    public static void main(String[] args) {
        log.info("Dummy Context (Not sure if this should ever run)");
    }

}

Integration test class:

package com.sodved.itestcase0.app.itest;

import com.sodved.itestcase0.app.service.AppService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes=DummyApplicationContext.class)
public class IntegrationIT {

    @Autowired
    private AppService appService;

    @Test
    public void testApplicationLoaded() {
        log.info("Application loaded OK");
    }

    @Test
    public void testAppSerivce() {
        log.info("App Service: {}", appService.getServiceName());
    }

}

Tests fail:

spring-itest> mvn clean verify -P itest -pl case0 -am -amd
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sodved.itestcase0.app.itest.IntegrationIT
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.sodved.itestcase0.app.itest.IntegrationIT
[ERROR] com.sodved.itestcase0.app.itest.IntegrationIT  Time elapsed: 0.002 s  <<< ERROR!
java.lang.NoClassDefFoundError: Lcom/sodved/itestcase0/app/service/AppService;
Caused by: java.lang.ClassNotFoundException: com.sodved.itestcase0.app.service.AppService

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   IntegrationIT » NoClassDefFound Lcom/sodved/itestcase0/app/service/AppService;
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

So I assume this is a simple stuffup by me (something wrong in my pom.xml?).
Some other points I have noted:

  • Only an issue for component classes defined in module being tested. It all works fine if the component class is from a dependency (see case1 in github).
  • As stated above, if I use Case0Application as the SpringBootTest class, then no tests are run. That is why I created the dummy context. You can see this in the case0-no-tests branch.

My example source code is available here: https://github.com/sodved/spring-itest

I've been banging my head against this for days. Read heaps of tutorials and manuals etc. It feels like a test config/classpath issue, but not sure if it's maven or spring boot where my mistake is. I cannot figure it out.

My real issue involves database containers etc, but this is the simplest case I could reproduce my issues with. Any help would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2025-01-29 18:56:20

您需要知道的第一件事是,Springboot会自动扫描软件包中相关类的@springbootapplication注释并将其注入容器中,或使用@ScanBasePackages注释来定义自定义扫描的位置,因此将加载DummyApplicationContext类。弹簧将帮助您加载com播种。 iTestcase0。应用程序。 iTest子袋类,因此无法找到AppService,您可以尝试将dummyApplicationCon文本类迁移到com。割了。 iTestcase0。应用程序包,然后看看是否有任何问题

The first thing you need to know is that SpringBoot will automatically scan the @SpringBootApplication annotation for the relevant classes in the package and inject them into the container, or use the @scanBasepackages annotation to define the location of the custom scan, so your test class will load DummyApplicationContext class. Spring will help you to load the com sodved. Itestcase0. App. Itest child bag class, so led to can not find AppService, you can try to put the DummyApplicationCon Text class migrated to com. Sodved. Itestcase0. App package, and then see if there are any problems

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文