MockitoJUnitRunner for junit in AemContext for ResourceBundle fails | Community
Skip to main content
srinivas_chann1
Level 7
August 7, 2020
Solved

MockitoJUnitRunner for junit in AemContext for ResourceBundle fails

  • August 7, 2020
  • 1 reply
  • 2295 views

Hi ,

 

I am using MockitoJUnitRunner for junit4 .I getting error when  the  code ResourceBundle that is there in component class when using junit. Please provide inputs as how to resolve the issue

 

Component class:-
import java.util.*;
public class ComponentClass extends WCMUsePojo{

public void init(final SlingHttpServletRequest slingRequest) {

slingRequest.getResourceBundle(locale).getString("TEST.STRING");
}

 

Junit class:-0

@RunWith(MockitoJUnitRunner.Silent.class)
public class testclass {

@1227241
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);


protected MockSlingHttpServletRequest request;

protected MockSlingHttpServletResponse response;

private final ComponentClass specSheetList = new ComponentClass();

@Test
public void testResourceBundle() {
ResourceBundle resourceBundle=request.getResourceBundle(locale);

//Throwing error here
resourceBundle.getString("TEST.STRING")
}


}

 

Error I getting:-
java.util.MissingResourceException: Can't find resource for bundle org.apache.sling.servlethelpers.MockSlingHttpServletRequest$1, key TEST.STRING
at java.util.ResourceBundle.getObject(ResourceBundle.java:450)
at java.util.ResourceBundle.getString(ResourceBundle.java:407)

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by srinivas_chann1

I was able to solve this 

 

/* ResourceBundle.getString(String) is a final method .So inorder to solve this
*issue we create a object and hardcode the required key and value */

In the above method 

we must replace key with TEST.STRING

 

import org.apache.sling.i18n.ResourceBundleProvider;
import static org.mockito.Mockito.when;
@Mock
ResourceBundleProvider resourceBundleProvider;
context.bundleContext().registerService(ResourceBundleProvider.class.getName(), resourceBundleProvider, null);
when(resourceBundleProvider.getResourceBundle("base1", Locale.US)).thenReturn(new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[][]{{"key", "value1"}};
}
});

ResourceBundle bundle = request.getResourceBundle("base1", Locale.US);
Locale locale = here get locale based on your code
when(request.getResourceBundle(locale)).thenReturn(bundle);

 

 

 

1 reply

srinivas_chann1
srinivas_chann1AuthorAccepted solution
Level 7
August 8, 2020

I was able to solve this 

 

/* ResourceBundle.getString(String) is a final method .So inorder to solve this
*issue we create a object and hardcode the required key and value */

In the above method 

we must replace key with TEST.STRING

 

import org.apache.sling.i18n.ResourceBundleProvider;
import static org.mockito.Mockito.when;
@Mock
ResourceBundleProvider resourceBundleProvider;
context.bundleContext().registerService(ResourceBundleProvider.class.getName(), resourceBundleProvider, null);
when(resourceBundleProvider.getResourceBundle("base1", Locale.US)).thenReturn(new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[][]{{"key", "value1"}};
}
});

ResourceBundle bundle = request.getResourceBundle("base1", Locale.US);
Locale locale = here get locale based on your code
when(request.getResourceBundle(locale)).thenReturn(bundle);