junit4 的使用 顺便理解ClassPathXmlApplicationContext的使用

来源:互联网 发布:python 字符串求和 编辑:程序博客网 时间:2024/05/20 09:07

工作中,需要给同事在dao层写个方法,写完后,只能用junit测试,如是学习了junit4的使用。

先用eclipse引入junit4相关包,然后写个类如下,就行了。

第一种方式:

public class Testextends TestCase{
     
    private IDiscountDao discountDao;
 
    @Override
    protected void setUp()throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        discountDao =context.getBean(IDiscountDao.class);
    }
     
    public void test1(){
        DiscountInfo info = discountDao.getDiscForZhuanban("036596782c9611e2b12d00215e6e7653");
        System.out.println(info);
    }
}

 框架用的是springmvc + ibatis ,各种类都是注解的。

discountDao =context.getBean(IDiscountDao.class);这样取到要测试的dao类。<br><br>ClassPathXmlApplicationContext("applicationContext.xml")这个表示取类路劲下的applicationContext.xml,即web-info/classes/下的applicationContext.xml文件,<br>也就是源码中src下面的applicationContext.xml文件。

第二种方式:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:conf/test-deploy.xml" })public class Freight {    @Autowired    private AreaFreightDao areaFreightDao;    public static void main(String[] args) {        BigDecimal a = BigDecimal.valueOf(1.0);        BigDecimal b = BigDecimal.valueOf(1.000);        if (a.compareTo(BigDecimal.ZERO) == 1) {//结果是true            System.out.println("1");        } else {            System.out.println("2");        }    }    @Test    public void test() {        BigDecimal a = areaFreightDao.queryAreaFreight(null, null, 120103);        System.out.println(a);    }



1 0
原创粉丝点击