spring 整合junit进行测试

    如果想让junit和spring容器环境无缝对接的话,可以使用如下方式:

  

import com.jd.ptest.service.ICronService;
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;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})  //此处读取spring的配置文件
public class CronServiceImplTest {

    @Autowired
    @SuppressWarnings("all")
    ICronService cronService;

    @Test
    public void addCron() {
        try
        {
            cronService.addCron();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

posted on 2018-05-08 18:27  babyblue  阅读(245)  评论(0编辑  收藏  举报