ssm项目中使用单元测试--junit

  1. 导入maven依赖
   
            org.springframework
            spring-test
            5.2.6.RELEASE
        

        
        
            junit
            junit
            4.12
        
  1. 编写测试类
// 表示继承了SpringJUnit4ClassRunner类
@RunWith(SpringJUnit4ClassRunner.class) 
 //加载spring容器
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})
public class test {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void test01(){
        List students = studentMapper.selectAllStudent();
        System.out.println(students);
    }
}

注意:

  • spring-test的大版本号要和你的spring版本对应。
  • ssm和springboot还有区别的。springboot 单元测试写完@test之后就可以测试。

但是ssm需要加上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})这两个注解。

你可能感兴趣的:(ssm项目中使用单元测试--junit)