Junit:如何测试从属性文件读取属性的方法 [英] Junit: How to test a method that read properties from a property file

查看:498
本文介绍了Junit:如何测试从属性文件读取属性的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类ReadProperty,该类具有返回类型Myclass的方法ReadPropertyFile,该方法从属性文件中读取参数值并返回Myclass对象.我需要帮助来使用JUnit测试ReadPropertyFile方法,如果可能的话,可以使用模拟文件和模拟对象.

Hi I have a class ReadProperty which has a method ReadPropertyFile of return type Myclass which read the parameter values from a property file and return Myclass object. I need help to test the ReadPropertyFile method with JUnit, if possible with mock files and mock object.

这是我的代码.

import java.io.FileInputStream;
import java.util.Properties;

public class ReadProperty {

    public Myclass ReadPropertyFile(String fileName) {
        Myclass myclass = null;
        String testparam = null;

        FileInputStream fis = null;



        Properties prop = new Properties();
        try {
            fis = new FileInputStream(fileName);
            try {
                prop.load(fis);
                System.out.println("Load Property file : Success !");
            } catch (Exception ex) {
                System.out.println("Load Property file : Exception : " + ex.toString());
            }
            /*
             * loading the properties
             */
            try {
                testparam = prop.getProperty("testparam");
                System.out.println("testparam Type : " + testparam);
            } catch (Exception ex) {
                System.out.println("testparam Type : " + ex.toString());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Property file read fail : " + ex.toString());
            System.exit(1);
        }
        Myclass = new Myclass(testparam);
        return Myclass;
    } }

推荐答案

我认为您真的不需要在这里嘲笑任何东西.您想测试属性读取器是否能够按预期访问和读取文件,因此请进行准确测试.对于常规属性,它可以像这样:

I don't think that you really need to mock anything here. You want to test if your property reader is able to access and read a file as you expect, so test exactly that. For regular properties it can go like this:

@Test
public void shouldReadPropFileFromSingleString() {

    final Properties p = PropertiesLoader
            .loadProperties("propfile");
    assertNotNull(p);
    assertFalse(p.isEmpty());
    for (final Entry<Object, Object> e : p.entrySet()) {
        assertEquals(expectedProperties.get(e.getKey()), e.getValue());
    }
}

对于您的情况,可以对其进行调整:

For your case, you can adapt it:

@Test
public void shouldReadCorrectProp() {

    final MyClass p = ReadProperty
            .readPropertyFile("propfile");
    assertNotNull(p);
    assertEquals(expectedProperty, p);
}

您可能还想测试悲伤的路径-如果找不到属性文件,是否有任何后备属性等会发生什么情况.

You may also want to test the sad path - what happens if the property file is not found, are any fallback properties available etc.

顺便说一句,我建议更改方法名称,因为读取属性文件不是方法的主要问题-检索属性是.更好的是,将该方法分解为getPropertyreadPropertyFile方法,其中第一个方法调用第二个方法.因此,根据关注的问题

BTW, I would advise changing the method name, since reading a property file is not the primary concern of your method - retrieving a property is. Better yet, decompose the method into a getProperty and a readPropertyFile method, where the first method calls the second. So you will have a cleaner design according to Separaton of Concerns

这篇关于Junit:如何测试从属性文件读取属性的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆