@ContextConfiguration的用法

@ContextConfiguration的用法

在Spring Boot测试

@ContextConfiguration这个注解通常与@RunWith(SpringJUnit4ClassRunner.class)联合使用用来测试

当一个类添加了注解@Component,那么他就自动变成了一个bean,就不需要在Spring配置文件中显示的配置了。把这些bean收集起来通常有两种方式,Java的方式和XML的方式。当这些bean收集起来之后,当我们想要在某个测试类使用@Autowired注解来引入这些收集起来的bean时,只需要给这个测试类添加@ContextConfiguration注解来标注我们想要导入这个测试类的某些bean。

这个@SpringBootTest注解意思就是将SpringBoot主类中导入的bean全都包含进来。

此时SpringBoot主类也被当作了bean的收集器

如下:

@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {IPMSExtendIApplication.class})
@SpringBootTest
public class MessageTest {
    @Autowired
    private IpmsMailService ipmsMailService;

    @Test
    public void emailTest() {
        String title = "测试邮件";
        String content = "内容是什么不重要,重要的是可以发出来";
        EmailDTO emailDTO = new EmailDTO();
        ArrayList<String> cc = new ArrayList<>();
        emailDTO.setCcUserList(cc);
        emailDTO.setToUserList(cc);
        emailDTO.setTitle(title);
        emailDTO.setContent(content);
        //发送邮件
        EmailResponseModel emailResponseModel = ipmsMailService.sendSimpleMail(emailDTO);
        System.out.println("=====" + emailResponseModel);

    }

}
posted @   码农公子的幸福生活  阅读(286)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
历史上的今天:
2022-01-29 调整服务器上的时间与本地一致,修复系统时区设置为EDT,比北京时间慢
点击右上角即可分享
微信分享提示