Chinaunix首页 | 论坛 | 博客
  • 博客访问: 369633
  • 博文数量: 100
  • 博客积分: 2586
  • 博客等级: 少校
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-09 15:20
个人简介

我是一个Java爱好者

文章分类

全部博文(100)

文章存档

2014年(2)

2013年(7)

2012年(2)

2010年(44)

2009年(28)

2008年(17)

我的朋友

分类: Java

2013-07-08 16:16:54

public class UsesResourceBundle {

 
private static Logger logger = LoggerFactory.getLogger(UsesResourceBundle.class);

 
private ResourceBundle bundle;

 
public String getResourceString(String key) {

   
if (isNull(bundle)) {
     
// Lazy load of the resource bundle
     
Locale locale = getLocale();

     
if (isNotNull(locale)) {
       
this.bundle = ResourceBundle.getBundle("SomeBundleName", locale);
     
} else {
       
handleError();
     
}
    }

   
return bundle.getString(key);
 
}

 
private boolean isNull(Object obj) {
   
return obj == null;
 
}

 
private Locale getLocale() {

   
return Locale.ENGLISH;
 
}

 
private boolean isNotNull(Object obj) {
   
return obj != null;
 
}

 
private void handleError() {
   
String msg = "Failed to retrieve the locale for this page";
    logger.error
(msg);
   
throw new RuntimeException(msg);
 
}
}


You can see that there’s one method: getResourceString(...), which given a key will retrieve a resource string from a bundle. In order to make this work a little more efficiently, I’ve lazily loaded my resource bundle, and once loaded, I call bundle.getString(key) to retrieve my resource. To test this I’ve written a PowerMock JUnit test:

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replayAll;
import static org.powermock.api.easymock.PowerMock.verifyAll;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(UsesResourceBundle.class)
public class UsesResourceBundleTest {

 
@Mock
 
private ResourceBundle bundle;

 
private UsesResourceBundle instance;

 
@Before
 
public void setUp() {
   
instance = new UsesResourceBundle();
 
}

 
@Test
 
public final void testGetResourceStringAndSucceed() {

   
mockStatic(ResourceBundle.class);
    expect
(ResourceBundle.getBundle("SomeBundleName", Locale.ENGLISH)).andReturn(bundle);

   
final String key = "DUMMY";
   
final String message = "This is a Message";
    expect
(bundle.getString(key)).andReturn(message);

    replayAll
();
    String result = instance.getResourceString
(key);
    verifyAll
();
    assertEquals
(message, result);
 
}

 
@Test(expected = MissingResourceException.class)
 
public final void testGetResourceStringWithStringMissing() {

   
mockStatic(ResourceBundle.class);
    expect
(ResourceBundle.getBundle("SomeBundleName", Locale.ENGLISH)).andReturn(bundle);

   
final String key = "DUMMY";
    Exception e =
new MissingResourceException(key, key, key);
    expect
(bundle.getString(key)).andThrow(e);

    replayAll
();
    instance.getResourceString
(key);
 
}

 
@Test(expected = MissingResourceException.class)
 
public final void testGetResourceStringWithBundleMissing() {

   
mockStatic(ResourceBundle.class);
   
final String key = "DUMMY";
    Exception e =
new MissingResourceException(key, key, key);
    expect
(ResourceBundle.getBundle("SomeBundleName", Locale.ENGLISH)).andThrow(e);

    replayAll
();
    instance.getResourceString
(key);
 
}

}


In the code above I’ve taken the unusual step of including the import statements. This is to highlight that we’re using PowerMock’s versions of the import statics and not EasyMock’s. If you accidentally import EasyMock’s statics, then the whole thing just won’t work.

There are four easy steps in setting up a test that mocks a static call:
  1. Use the PowerMock JUnit runner:

    @RunWith(PowerMockRunner.class)

  2. Declare the test class that we’re mocking:

    @PrepareForTest(UsesResourceBundle.class)

  3. Tell PowerMock the name of the class that contains static methods:

    mockStatic(ResourceBundle.class);

  4. Setup the expectations, telling PowerMock to expect a call to a static method:

    expect(ResourceBundle.getBundle("SomeBundleName", Locale.ENGLISH)).andReturn(bundle);


The rest is plain sailing, you set up expectations for other standard method calls and the tell PowerMock/EasyMock to run the test, verifying the results:

    final String key = "DUMMY";
   
final String message = "This is a Message";
    expect
(bundle.getString(key)).andReturn(message);

    replayAll
();
    String result = instance.getResourceString
(key);
    verifyAll
();
阅读(1735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~