@WebMvcTest 由于缺少依赖项而未运行

2024-04-06

我想使用以下方法测试我的控制器@WebMvcTest. I @MockBean控制器的依赖项,但运行测试时,无法启动。运行主类时应用程序正确启动。

考试:

@RunWith(SpringRunner.class)
@WebMvcTest(MetricResource.class)
public class MetricResourceTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private MetricService metricService;

    @MockBean
    private MetricMapper metricMapper;

    @Test
    public void test() {
    }

}

控制器:

@RestController
@RequestMapping("/api/v1/metrics")
public class MetricResource {

    private final MetricService metricService;

    private final MetricMapper metricMapper;

    public MetricResource(MetricService metricService, MetricMapper metricMapper) {
        this.metricService = metricService;
        this.metricMapper = metricMapper;
    }

    @GetMapping
    public ResponseEntity<List<MetricDto>> getMetrics(@RequestParam(required = false) List<String> fields) {
        if (fields == null) {
            fields = new ArrayList<>();
        }
        List<Metric> metrics = metricService.getMetric(fields);
        List<MetricDto> dto = metricMapper.fromMetric(metrics);
        return ResponseEntity.ok(dto);
    }

}

错误:

Description:

Parameter 2 of constructor in com.sps.soccer.service.SoccerService required a bean named 'mongoTemplate' that could not be found.

Action:

Consider defining a bean named 'mongoTemplate' in your configuration.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'soccerService' defined in file [K:\home\projects\stable\sps-backend\sps-soccer\target\classes\com\sps\soccer\service\SoccerService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerAnalysisRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerAnalysisRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

The SoccerService依赖于SoccerAnalysisRepository这是一个MongoRepository。我不明白为什么SoccerService是由测试创建的,因为@Service未被扫描@WebMvcTest.

该应用程序是一个 Maven 多模块,因此我必须显式配置组件扫描和存储库。

@SpringBootApplication
@ComponentScan(basePackages = {"com.sps.soccer", "com.sps.sdql", "com.sps.core", "com.sps.web"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.sdql.configuration.ClockConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.soccer.configuration.ClockConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.sdql.configuration.RestConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.soccer.configuration.RestConfiguration.class)
        })
@EnableMongoRepositories(basePackages = {"com.sps.soccer", "com.sps.sdql", "com.sps.core"})
public class SpsWebApplication {

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

您必须移动所有特定于区域的配置,例如@ComponentScan and @EnableMongoRepositories, 到一个单独的@Configuration文件。重要的是不要在应用程序的主类中添加特定于其功能的特定区域的配置设置。

更多信息:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-user-configuration https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-user-configuration

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@WebMvcTest 由于缺少依赖项而未运行 的相关文章

随机推荐

  • 如何解决 SceneKit double notsupported 错误?

    过去几天我一直在研究 iOS 版 SceneKit 我在尝试创建自定义几何图形时遇到了一个问题 每当我尝试显示几何图形时 它都不会绘制 并在运行时向我显示此错误 SceneKit 错误 C3DRendererContextSetupResi
  • C# 无法从应用程序成功启动中打开串行端口

    因此 我正在打开这个端口并将端口名称保存在我的应用程序设置中 当我加载表单时 它尝试打开端口 但失败了 仅在单击我的 开放端口 按钮后EXACT端口打开时使用相同的代码 然后 如果我单击 关闭端口 按钮并重新打开表单 它就可以工作了 它会自
  • System.Windows.Forms.HtmlDocument 不包含 GetElementByID 的定义

    您能解释一下为什么我会收到此错误吗 private void startButton Click object sender EventArgs e HtmlElement mainNav webBrowser1 Document GetE
  • Perl 中的列表运算符优先级

    我正在读 Beginning Perl 一书 它给出了以下两条语句 print Test one 6 gt 3 3 gt 4 n print Test two 6 gt 3 and 3 gt 4 n 第一行不打印任何内容并换行 第二行打印
  • 创建向量空间

    我有一个问题 我有很多文档 每一行都是由某种模式构建的 当然 我有这一系列的图案 我想创建一些向量空间 然后通过某种规则来向量这个模式 我还不知道这个规则是什么 即使这个模式像我的向量空间的 质心 然后向量当前文档的每一行 再次按照此规则
  • 如何在 .NET Core 应用程序 Docker 映像中包含依赖项?

    我正在尝试构建 NET Core 应用程序 Docker 映像 但我不知道如何将项目的 NuGet 依赖项获取到图像中 为简单起见 我创建了一个 NET Core 控制台应用程序 using System using Newtonsoft
  • 使用 C# 生成随机数

    我正在考虑生成 1 到 500 万之间的随机数 这个过程不必很快 尽管如果快的话就好了 但它必须尽可能随机 我知道没有什么是随机的 我有多种种子数据源 我不确定是否 NET http en wikipedia org wiki NET Fr
  • 在 Javascript 中将变量名转换为字符串?

    我在 Stack Overflow 上看到过一些关于此问题的其他帖子 但答案似乎总是创建一个带有键 值对的对象 这似乎不是我目前的情况所需要的 我想要做什么 我有不同的数组 其中可能包含用户名 我想检查每个数组 看看用户名是否作为其中的值存
  • 哪些错误适用于不同的 FindBugs 报告级别?

    根据 Ant 任务 报告级别是低 中 高设置 用于查找不同严重程度级别的错误 然而 我找不到任何东西可以解释哪些错误适用于这些不同的报告级别 是否有某个地方可以提供此功能 即使我需要在源代码中查看它 不幸的是 错误类型和优先级之间的关联存在
  • SwiftUI UIViewRepresentable UITextView 绑定

    目前 SwiftUI 本身不支持多行文本输入 希望很快就会添加此功能 所以我一直在尝试使用组合框架来实现 UIKit 中的 UITextView 它支持多行输入 但是我得到的结果好坏参半 这是我创建的用于制作文本视图的代码 struct M
  • 使用默认值初始化方法的参数

    如果未将显式值传递到方法中 我想用一些默认值初始化方法的参数 如下所示 class Example def init self data self default data self data data def default data r
  • Safari 上使用 React 的日期选择器

    我的应用程序使用 Form Input 来自语义 UI 反应 https react semantic ui com collections form 库插入日期 它在 Chrome 和 Firefox 上显示日期选择器 但在 Safari
  • 在 django-oscar 中集成包含重定向的付款方式

    我正在使用 django oscar 框架开发一个购物网站 实际上我正在使用他们的沙箱网站 我想在结帐流程中添加付款功能 但问题是 我完全困惑了 我已阅读此链接 奥斯卡的支付集成文档 http django oscar readthedoc
  • 如何摆脱 Eclipse 查找/替换栏

    For searching a document I like using the floating Find Replace window that pops up Lately however hitting command F pul
  • 使用 Phonegap 启动图像后白屏闪烁

    加载启动图像后 在加载应用程序之前我会看到一个白屏 我正在将phonegap 与xcode 结合使用 我希望我的应用程序加载到我的启动图像上 以便它遵循 ios 开发人员指南并提供流畅的启动体验 我认为这个白屏是我的应用程序加载的网络视图
  • 从具体类派生抽象类

    假设我们有一个具体的class Apple Apple 对象可以被实例化 现在 有人来导出一个摘要class Peach来自苹果 它是抽象的 因为它引入了新的纯虚函数 Peach 的用户现在被迫从它派生并定义这个新函数 这是常见的模式吗 这
  • 将模型引用添加到现有 Rails 模型

    我想知道在两个之间添加关系的 正确 方法existingRails 3 中的类 给定现有模型 小丑和兔子 我想添加从兔子到小丑的引用 belongs to 我首先尝试生成迁移 rails g migration AddClownToRabb
  • SwitchPreferenceCompat:android:switchTextOff / switchTextOn 不起作用

    我正在尝试显示一个 switchPreference 它允许用户根据英里或公里显示距离 我正在使用 SwitchPreferenceCompat 支持库 根据该库 我可以使用 textSwitchOff 和 textSwitchOn 将文本
  • 如何使用 SwiftUI 获得动态视图列表

    我可以做一个静态列表 比如 List View1 View2 但是如何从数组中创建元素的动态列表呢 我尝试了以下但出现错误 包含控制流语句的闭包不能与函数生成器 ViewBuilder 一起使用 let elements Any View1
  • @WebMvcTest 由于缺少依赖项而未运行

    我想使用以下方法测试我的控制器 WebMvcTest I MockBean控制器的依赖项 但运行测试时 无法启动 运行主类时应用程序正确启动 考试 RunWith SpringRunner class WebMvcTest MetricRe