Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

MockitoJUnitRunner for junit in AemContext for ResourceBundle fails

srinivas_chann1
Level 6
Level 6

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 {

@rule
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

1 Accepted Solution
srinivas_chann1
Correct answer by
Level 6
Level 6

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);

 

 

 

View solution in original post

1 Reply
srinivas_chann1
Correct answer by
Level 6
Level 6

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);

 

 

 

View solution in original post