Spring整合junit

1、导入pom坐标

XML 复制代码
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

2、编写测试用例

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest {
    @Autowired
    private AccountService accountService;

    @Test
    public void TestFindById(){
        System.out.println( accountService.findById(1));
    }
}
  • @RunWith 指定测试用例类型
  • @ContextConfiguration 指定测试SpringConfig配置类
  • @Autowired 自动装配
  • @Test 表明这个是测试用例
相关推荐
摇滚侠39 分钟前
2025最新 SpringCloud 教程,Seat-原理-四种事务模式,总结,笔记72,笔记73
笔记·spring·spring cloud·架构
掘金-我是哪吒1 小时前
第378集设备服务接入系统Java微服务后端架构实战
java·开发语言·spring·微服务·架构
凛冬君主2 小时前
探索 Spring 的基础依赖与 Spring Boot 依赖
java·spring boot·spring
摇滚侠3 小时前
2025最新 SpringCloud教程,Seata-基础-环境搭建,笔记64
笔记·spring·spring cloud
Li_7695323 小时前
Spring Cloud —— SkyWalking (二)
spring·spring cloud·skywalking
雨落秋垣3 小时前
优化 OpenResty 的性能
junit·openresty
雨落秋垣3 小时前
SpringCache 缓存:注意事项、问题解决与优化策略
java·spring·缓存
想不明白的过度思考者3 小时前
Spring IoC 与 DI 深度剖析:从“控制反转”到 Bean 的集中管理
java·spring·ioc·di
小马爱打代码4 小时前
Spring AI:RAG 增强检索介绍
java·人工智能·spring
网硕互联的小客服14 小时前
MYSQL数据库和MSSQL数据库有什么区别?分别适用于什么脚本程序?
数据库·mysql·sqlserver