java - 如何将 ActiveProfiles 与带有 WebMvcTest 的 Spring Boot 测试一起使用?
问题描述
我有一个单元测试正在使用@WebMvcTest
. 控制器类自动装配我想模拟的服务类。我发现我可以使用@Profile
并@Configuration
创建一个配置类来指定要在配置文件处于活动状态时使用的主 bean。我尝试向我的单元测试类添加一个活动配置文件,但它说它无法加载 ApplicationContext。我不确定在使用@WebMvcTest
.
@ActiveProfiles("mockServices")
@RunWith(SpringRunner.class)
@WebMvcTest(VoteController.class)
public class VoteControllerTest {
...
看来我可能正在接近这个错误。任何帮助表示赞赏。
编辑:
这是我的配置类:
@Profile("mockService")
@Configuration
public class NotificationServiceTestConfiguration {
@Bean
@Primary
public VotingService getVotingService() {
return Mockito.mock(VotingService.class);
}
}
我实际得到的错误是:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“VotingService”类型的合格 bean
我能够通过在我的单元测试类中使用@MockBean
for VotingService 来解决它。但是,我想使用我在 NotificationServiceTestConfiguration 中的配置文件配置,而不必调用模拟 bean。
有什么想法吗?
解决方案
推荐阅读
- python - 递归替换python3字典中的字符
- javascript - 无法在 Deno 中加载依赖的 txt 文件
- android - 如何实现砖墙式布局以显示列表?- 安卓工作室
- solr - 删除停用词html标签并计算句子中单词的频率
- python - 将列表中的 n 个数字相加并返回总数
- twig - 用于 table.html.twig 的 drupal 8 模板建议
- c - 未使用的参数“状态”的用途是什么?
- java - springdata jpa中的OneToMany关系与外键也是复合主键的一部分
- node.js - Angular 应用程序无法通过 Socket.IO 与 Flask 通信
- angular - 如何使用 ngModel 在 div 的 innerhtml 属性上实现 2 路数据绑定