Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock getResourceAsStream( ) method

I have a method call like this

class classname
    {
    void somemethod()
    {
    InpuStream someImputStream=classname.class.getResourceAsStream("some string");
    }
    }

Is there any way to mock the method call?

Thanks

like image 611
user3827560 Avatar asked Oct 20 '22 05:10

user3827560


1 Answers

You can first introduce a method for returning the input stream

 class classname
        {
        void somemethod()
        {
           InpuStream someImputStream = getInputStream();
        }

        protected InputStream getInputStream() {
            return classname.class.getResourceAsStream("some string");
        }


        }

In your test case

classname testObject = org.mockito.Mockito.spy(new classname());
org.mockito.Mockito.when(testObject.getInputStream()).thenReturn(...);
like image 60
Dev Blanked Avatar answered Oct 27 '22 11:10

Dev Blanked