Java_UT_Mock系列之-03Powermock与配置类Property(1)

说明

目前还是一个半成品。存在的问题: 需要先通过PropertiesClass构造方法来导入一个dummy的配置文件,然后再修改具体的Propery值。
这对于有大量需要初始化配置的应用来说,不太实用。需要进一步研究如何去mock
getResourceAsStream来完成属性的动态设置。

测试用例

//loading from a real property file

@Test
public void testPropertiesClass() {
PropertiesClass pc= new PropertiesClass();
assertEquals("111",pc.print("aaa"));
}
//loading from a defult file and then modify the properties.
@Test
public void testMockPropertiesClassWithDummy() {
Properties mockProperties =PowerMockito.mock(Properties.class);
PropertiesClass propertiesClass = new PropertiesClass();
Whitebox.setInternalState(propertiesClass, "prop", mockProperties);
        PowerMockito.when(mockProperties.getProperty("aaa")).thenReturn("8888");
assertEquals("8888",propertiesClass.print("aaa"));
}

SUT:

public class PropertiesClass {
private Properties prop= new Properties();
public PropertiesClass() {
load();
}
public void load()  {
prop= new Properties();
InputStream in;
try {
in = this.getClass().getResourceAsStream("a.properties");
prop.load(in);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String print(String key) {
return prop.getProperty(key);
}
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,184评论 19赞 139
  • 1 Webpack 1.1 概念简介 1.1.1 WebPack是什么 1、一个打包工具 2、一个模块加载工具 3...
    Kevin_Junbaozi阅读 7,078评论 0赞 16
  • 自女儿上小学以来,总是能听到一些家长之间对现今教育的怨声载道,批判不满又无可奈何。这么小就这么作业繁重,以后可怎么...
    周雪2229阅读 1,228评论 0赞 3
  • 今天的主题是理财工具 主流理财工具包括:国债、国债逆回购、银行理财、理财型保险、货币基金、网贷P2P基金、基金、股...
    顿悟的鱼88阅读 331评论 0赞 1
  • 每天三件事 1、线下分享活动 2、晚上组织文字分享 3、听音乐,打太极 小确幸 认识新朋友,开启人生超链接。 感悟...
    Katrina程阅读 145评论 0赞 0

友情链接更多精彩内容