spring-test测试demo

如果是maven项目,pom中增加如下依赖:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.1.1.RELEASE</version>
</dependency>

import org.apache.log4j.Logger;
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 com.alibaba.fastjson.JSON;
import com.joey.model.InfoCfg;
import com.joey.model.UrUser;
import com.joey.service.InfoCfgServiceI;
import com.joey.service.UrUserServiceI;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class TestMybatis {

    //public ApplicationContext ac;
    private static final Logger logger = Logger.getLogger(TestMybatis.class);

    private InfoCfgServiceI infoService;
    private UrUserServiceI uruserService;

/*    @Before
    public void before(){
        ac=new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
        uruserService = (UrUserServiceI)ac.getBean("uruserService");
        infoService = (InfoCfgServiceI)ac.getBean("infoService");

    }*/
    
    @Test
    public void test1() {
        InfoCfg u = infoService.get****(2985l); //相关业务方法
        logger.info(JSON.toJSONStringWithDateFormat(u, "yyyy-MM-dd HH:mm:ss"));
    }
    
    @Test
    public void testUser() {
        UrUser u = uruserService.******(285l);  //相关业务方法
        //logger.info(JSON.toJSONStringWithDateFormat(u, "yyyy-MM-dd HH:mm:ss"));
        logger.info(u.getUserId()+",,,,"+u.getCompany());
    }

    public InfoCfgServiceI getInfoService() {
        return infoService;
    }

    @Autowired
    public void setInfoService(InfoCfgServiceI infoService) {
        this.infoService = infoService;
    }

    public UrUserServiceI getUruserService() {
        return uruserService;
    }

    @Autowired
    public void setUruserService(UrUserServiceI uruserService) {
        this.uruserService = uruserService;
    }
}

posted @ 2016-12-16 09:52  西凤楼  阅读(277)  评论(0编辑  收藏  举报
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。 感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。 因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询!