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

  1. 导入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>
  1. 编写测试类
// 表示继承了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"})这两个注解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容