Mock getResourceAsStream( ) method

junit, mockito, powermock, unit-testing

Solution

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

Problem

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

Original source