首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >SpringBoot 整合 Junit 单元测试

SpringBoot 整合 Junit 单元测试

作者头像
Yuyy
发布2022-06-28 19:48:24
发布2022-06-28 19:48:24
83800
代码可运行
举报
运行总次数:0
代码可运行

本文最后更新于 894 天前,其中的信息可能已经有所发展或是发生改变。

1. pom.xml

  • Eclipse创建时需要手动加依赖坐标
代码语言:javascript
代码运行次数:0
运行
复制
<!- 添加 junit 环境的 jar 包 ->
<depndency>
    <groupId>org.springframework.bot</groupId>
    <artifactId>spring-bot-starter-test</artifactId>
</depndency>
  • IDEA创建SpringBoot项目会自动导入
代码语言:javascript
代码运行次数:0
运行
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

2. 生成测试文件

快捷键:Ctrl+Shift+T @RunWith:启动器 SpringJUnit4ClasRuner.clas:让 junit 与 spring 环境进行整合 @SpringBotTest(clases={Ap.clas}) 1,当前类为 springBot 的测试类 @SpringBotTest(clases={Ap.clas}) 2,加载 SpringBot 启动类。启动springBot @Contextconfiguartion("claspath:aplicationContext.xml") junit 与 spring 整合

代码语言:javascript
代码运行次数:0
运行
复制
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {SpringBootTestApplication.class})
public class UserServiceImplTest {

    @Autowired
    private UserServiceImpl userService;

    @Test
    public void addUser() {
        userService.addUser();
    }
}

Post Views: 453

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-1-16 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. pom.xml
  • 2. 生成测试文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档