简单易懂的springMVC中的测试类

时间:2022-07-28
本文章向大家介绍简单易懂的springMVC中的测试类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.依赖导入

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2.代码示例

package test;

import entity.Product;
import mapper.ProductMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * zt
 * 2020/10/12
 * 21:38
 */
//@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//用于测试加载spring环境常与@RunWith联用
@ContextConfiguration(locations = "classpath:application.xml")
public class TestMapper {
    @Autowired
    private ProductMapper mapper;
    @Test
    public void test1(){
        Product p = new Product();
        p.setTid(1);
        p.setPname("aaa");
        mapper.add(p);
    }

    @Test
    public void test2(){
        mapper.delete(2);
    }
    @Test
    public void test3(){
        System.out.println(sysUsersMapper.findRole(1));
    }

}

3.运行结果