- 导入maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
- 编写测试类
// 表示继承了SpringJUnit4ClassRunner类
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring容器
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})
public class test {
@Autowired
private StudentMapper studentMapper;
@Test
public void test01(){
List<Student> students = studentMapper.selectAllStudent();
System.out.println(students);
}
}
注意:
- spring-test的大版本号要和你的spring版本对应。
- ssm和springboot还有区别的。springboot 单元测试写完@test之后就可以测试。
但是ssm需要加上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})这两个注解。