Spring学习二:spring-整合junit

本功能使用依赖:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

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

spring整合junit思路:

1. 让springJunit负责创建Spring容器,但是需要将配置文件告诉springJunit
2. 将需要进行测试的Bean注入到测试类中

spring整合junit步骤:

1. 导入spring集成junit的坐标
2. 使用@Runwith注解替换原来的运行期。即:之前运行找junit,现在找spring,让spring找junit。
3. 使用@ContextConfiguration注解指定配置文件或者配置类
4. 使用@Autowired注入需要测试的对象
5. 编写测试方法

对比:

posted @ 2022-01-18 21:26  扬帆去远航  阅读(127)  评论(0)    收藏  举报