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 {
@Deleted Account
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
Solved! Go to Solution.
Views
Replies
Total Likes
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);
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);
Views
Likes
Replies