1.需要引入的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.0.RELEASE</version> </dependency>
2.单元测试的代码
import com.woniu.erp.Mapper.GoodsManagerMapper; import com.woniu.erp.entity.Good; 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; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)//固定的内容 @ContextConfiguration(locations = "classpath:config/SpringContextConfiguration.xml")//这个引用spring的主配值文件,注意路径要正确 public class Test { @Autowired GoodsManagerMapper goodsManagerMapper ;//依赖注入 @org.junit.Test public void test(){ List<Good> goodsList = goodsManagerMapper.getGoodsList(); //调用方法测试 System.out.println(goodsList); } }
总结:1.需要引入spring单元测试的依赖
2.需要在类上面加@RunWith注解和@ContextConfiguration注解
3.对属性进行依赖注入
4.在test中调用方法测试
原文:https://www.cnblogs.com/yyk520/p/13369380.html