JiuyeXD's Blog
九叶
九叶博主

越努力 越幸运

登录
夜间

SSM搭建Spring单元测试环境

步骤

  1. 导入SpringTest模块
  2. @ContextConfiguration注解指定Spring配置文件的位置 自动创建IOC容器
  3. @RunWith指定spring单元测试模块
  4. 直接使用@Autowired或@Resource注解注入需要测试的组件
1.1 导入Spring-test jar包

下载地址: https://mvnrepository.com/artifact/org.springframework/spring-test

maven: 在pom.xml中添加

<dependency>
    <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>
1.2 @ContextConfiguration指定Spring配置文件的位置

image.png

1.3 直接使用@Autowired或@Resource注解注入组件

image.png

1.4编写测试代码

image.png

1.5运行测试一下

image.png

THE END