2

I have a i18n properties file called as

MessagesBundle_en.properties

invalidresourceBundle = Default locale English
invalidUserName = UserName cannot be null or empty or blank string

I have a class called ParameterVerify which verifies the parameters.

public class ParameterVerify {

    public void verifyParameters(String userName, ModelAndView modelAndView, ResourceBundle resourceBundle) {

        if (resourceBundle == null) {
            resourceBundle = ResourceBundle.getBundle("MessagesBundle", Locale.ENGLISH);
        }

        if (!(verifyParameter(userName))) {
            modelAndView.addObject("invalidUserName", resourceBundle.getString("invalidUserName"));
        }
    }

    private boolean verifyParameter(final String parameter) {
        if (!(StringUtils.isBlank(parameter))) {
            return true;
        } else {
            return false;
        }
    }
}

How to Test and Mock the method of the class ParameterVerify?

YourAboutMeIsBlank
  • 1,787
  • 3
  • 18
  • 27

1 Answers1

2

I've hacked a unit test for you. I´m using Mockito to create a mock for ModelAndView. Mocking the ResourceBundle isn't possible as the getString() method is final. So I´m using a dummy implementation to manipulate the return value. If you have a number of those tests you should better wrap the ResourceBundle in an own class which is mockable and pass that around. But wait... you won't need to mock or stub the RessourceBundle as you can use the real .properties file and pass null here.

I wrote this test just to get you an idea about how this could work.

@Test
public void test() {
    String userName = null; // should add message
    ModelAndView modelAndView = Mockito.mock(ModelAndView.class);
    ResourceBundle resourceBundle = new ResourceBundle() {
        @Override
        protected Object handleGetObject(String key) {
            if ("invalidUserName".equals(key)) {
                return "mocked_string";
            }
            return null;
        }

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

    ParameterVerify parameterVerify = new ParameterVerify();
    parameterVerify.verifyParameters(userName, modelAndView, resourceBundle);
    Mockito.verify(modelAndView, Mockito.times(1)).addObject(Mockito.eq("invalidUserName"), Mockito.eq("mocked_string"));
}

Better use the real MessagesBundle_en.properties file as test resource:

@Test
public void test() {
    String userName = null; // should add message
    ModelAndView modelAndView = Mockito.mock(ModelAndView.class);
    ResourceBundle resourceBundle = null;

    ParameterVerify parameterVerify = new ParameterVerify();
    parameterVerify.verifyParameters(userName, modelAndView, resourceBundle);
    Mockito.verify(modelAndView, Mockito.times(1)).addObject(Mockito.eq("invalidUserName"), Mockito.eq("UserName cannot be null or empty or blank string"));
}
Community
  • 1
  • 1
Kai
  • 38,985
  • 14
  • 88
  • 103