spring使用@Test——阅读开源项目中的代码

 

目的:不建立Main方法测试相应的模块。

示例:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)     //指定测试工具
@ContextConfiguration(classes = TestConfig.class)   //指定主配置文件
@ActiveProfiles("dev")                      //设置配置环境名称(应用于区分配置环境的时候)
public class DemoBeanIntergration {

    @Autowired
    TestBean testBean;

    @Test
    public void prodBeanShouldInject(){
        String content = testBean.getContent();
        System.out.println(content);
        Assert.assertEquals("dev",content);
    }
}

 

posted @ 2020-05-23 21:08  怪兽不纯粹  阅读(264)  评论(0)    收藏  举报