单元测试使用资源包的静态方法 [英] Unit testing static method which uses a resource bundle

查看:203
本文介绍了单元测试使用资源包的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于使用Powermock和Mockito的文章并尝试了很多不同的方法,但我仍然无法弄清楚如何对下面的静态方法进行单元测试。

  public static Map< String,String> getEntries(){
Map< String,String> myEntriesMap = new TreeMap< String,String>();
ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE);
枚举< String> enumList = myEntries.getKeys();
String key = null;
String value = null;
while(enumList.hasMoreElements()){
key = enumList.nextElement()。toString();
value = myEntries.getString(key);
myEntriesMap.put(key,value);
}
返回myEntriesMap;
}

代码是包含约30个静态方法的(遗留)类的一部分这和重构不是一个真正的选择。类似地,在一些其他静态方法中,正在检索DB连接。



例如:如何模拟资源包ENTRIES_BUNDLE并对此方法进行单元测试?
我正在寻找一种通常适用于所有静态方法的模式。

解决方案

使用ResourceBundle.getBundle (String,ResourceBundle.Control)获取ResourceBundle以缓存给定String的包。您可以将ResourceBundle.Control子类化,以提供您心中所需的任何类型的包。

  @Test 
public void myTest( )
{
//在你的测试的初始阶段运行一个初始的getBundle()用你的控件调用
//。这将导致ResourceBundle缓存结果。
ResourceBundle rb1 = ResourceBundle.getBundle(blah,myControl);

//现在没有提供控件的调用仍将返回
//你的模拟包。好极了!
ResourceBundle rb2 = ResourceBundle.getBundle(blah);
}

这是子类控件:



'pre> 的ResourceBundle.Control myControl =新的ResourceBundle.Control()
{
公开的ResourceBundle newBundle(字符串baseName的,区域设置区域设置,字符串格式,
ClassLoader loader,boolean reload)
{
return myBundle;
}
};

这是模拟ResourceBundle的一种方法(根据需要使用键/值填充TreeMap)单元测试留给读者练习):

  ResourceBundle myBundle = new ResourceBundle()
{
protected void setParent(ResourceBundle parent)
{
//覆盖什么都不做,否则ResourceBundle.getBundle(String)
//进入无限循环!
}

TreeMap< String,String> tm = new TreeMap< String,String>();

@Override
protected Object handleGetObject(String key)
{
return tm.get(key);
}

@Override
public Enumeration< String> getKeys()
{
返回Collections.enumeration(tm.keySet());
}
};


Ive read so many articles on using Powermock and Mockito and tried so many different ways, but I still cant figure out the way to unit test the below static method.

public static Map<String, String> getEntries() {
    Map<String, String> myEntriesMap = new TreeMap<String, String>();
    ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE);
    Enumeration<String> enumList = myEntries.getKeys();
    String key = null;
    String value = null;
    while (enumList.hasMoreElements()) {
        key = enumList.nextElement().toString();
        value = myEntries.getString(key);
        myEntriesMap.put(key, value);
    }
    return myEntriesMap;
}

The code is part of a (legacy) class containing about 30 static methods like this and refactoring is not really an option. Similarly in some other static methods, DBconnections are being retrieved.

Eg : How do I mock the resource bundle ENTRIES_BUNDLE and unit test this method ? I am looking for a pattern that could be applied generally to all the static methods.

解决方案

Use ResourceBundle.getBundle( String, ResourceBundle.Control ) to get ResourceBundle to cache a bundle for the given String. You can subclass ResourceBundle.Control to provide any type of bundle your heart desires.

@Test
public void myTest()
{
    // In your Test's init phase run an initial "getBundle()" call
    // with your control.  This will cause ResourceBundle to cache the result.
    ResourceBundle rb1 = ResourceBundle.getBundle( "blah", myControl );

    // And now calls without the supplied Control will still return
    // your mocked bundle.  Yay!
    ResourceBundle rb2 = ResourceBundle.getBundle( "blah" );
}

Here's the subclassed Control:

ResourceBundle.Control myControl = new ResourceBundle.Control()
{
    public ResourceBundle newBundle( String baseName, Locale locale, String format,
            ClassLoader loader, boolean reload )
    {
        return myBundle;
    }
};

And here's one way to mock the ResourceBundle (filling up the TreeMap with keys/values as needed for the unit tests left as an exercise for the reader):

ResourceBundle myBundle = new ResourceBundle()
{
    protected void setParent( ResourceBundle parent )
    {
      // overwritten to do nothing, otherwise ResourceBundle.getBundle(String)
      //  gets into an infinite loop!
    }

    TreeMap<String, String> tm = new TreeMap<String, String>();

    @Override
    protected Object handleGetObject( String key )
    {
        return tm.get( key );
    }

    @Override
    public Enumeration<String> getKeys()
    {
        return Collections.enumeration( tm.keySet() );
    }
};

这篇关于单元测试使用资源包的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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