美文网首页
ssm项目中使用单元测试--junit

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

作者: Summer2077 | 来源:发表于2020-06-06 07:56 被阅读0次
  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"})这两个注解。

相关文章

网友评论

      本文标题:ssm项目中使用单元测试--junit

      本文链接:https://www.haomeiwen.com/subject/rruzzhtx.html