Mock ResourceBundle with some string value

4,456 views
Skip to first unread message

Raja R

unread,
Apr 11, 2013, 4:46:03 AM4/11/13
to moc...@googlegroups.com
Hi,

I want to initialize ResourceBundle mock with one resource, for example when(resourceBundle.getString("local")).thenReturn("Sample");

In this situation i am getting "java.util.MissingResourceException: Can't find resource for bundle $java.util.ResourceBundle$$EnhancerByMockitoWithCGLIB$$e9ea44f0, key local" Exception.

Can you guys help me to implement this?
Regards,
Raja 

Eric Lefevre-Ardant

unread,
Apr 11, 2013, 4:49:47 AM4/11/13
to moc...@googlegroups.com
Could you provide excerpts from your code? 


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Brice Dutheil

unread,
Apr 11, 2013, 5:40:24 AM4/11/13
to moc...@googlegroups.com
Hi Eric,


Cheers,
-- Brice

Eric Lefevre-Ardant

unread,
Apr 11, 2013, 8:17:23 AM4/11/13
to moc...@googlegroups.com
It seems to me that the question has been answered there.
Raja, let us know if you have further questions.

Raja R

unread,
Apr 16, 2013, 7:29:49 AM4/16/13
to moc...@googlegroups.com
Thanks guys. The issue is resolved. I have created the ResourceBundle instance and passed to the method.

somya kumari

unread,
Jun 28, 2018, 6:04:15 AM6/28/18
to mockito

Hi Raja
I am getting same error in mocking the ResourceBundle ,
This my class..
@RequestMapping(value = "/downLoadFile", method = RequestMethod.GET)
    public void downLoadFile(HttpServletRequest request,
            HttpServletResponse response) {
        try {
            String id = request.getParameter("id");
            ResourceBundle RESOURCE = ResourceBundle.getBundle("automation_messages");
            String filePath = RESOURCE.getString("filedestination");
            String fileName = request.getParameter("fileName");
            logger.debug("file name is.." + fileName);
            File file = new File(filePath + "//" + id + "/" + fileName);
            InputStream in = new BufferedInputStream(new FileInputStream(file));
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
            ServletOutputStream out = response.getOutputStream();
            IOUtils.copy(in, out);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



this is my testcase


@Test
    public void downLoadFileTest() throws IOException {
       


        String filePath = "C:/migrationfiles";
        String fileName ="Textfile.txt";
        String id ="s11";
       
        Mockito.when(req.getParameter(Mockito.anyString())).thenReturn(id);   
        ResourceBundle resourceBundle = PowerMockito.mock(ResourceBundle.class);
        Mockito.when(resource.getString(Mockito.anyString())).thenReturn(filePath);
        //Mockito.when(req.getParameter(Mockito.anyString())).thenReturn(fileName);
        Mockito.when(req.getParameter(Mockito.anyString())).thenReturn(fileName);
       
        Mockito.when(res.getOutputStream()).thenReturn(out);
       
        fd.downLoadFile(req, res);
       
        withSuccess();
       
       
    }
can you please help me?
Reply all
Reply to author
Forward
0 new messages