I have a class where Runtime.getRuntime() is used for executing a script from command line and getting the result for further processing.
But when I write JUnit for this class, I cannot find a way to mock/avoid this Runtime.getRuntime().exec() .
I cannot use EasyMock or PowerMock or any other mock api other than Mockito.
Please give me a way to overcome this problem, because this is affecting the code coverage.
You must refactor. Extract
Runtime.getRuntime().exec()into a separate class:Now insted of calling
getRuntime()explicitly injectShellclass somehow to your class under test:In JUnit test simply inject mocked
Shellclass by passing it to constructor:Sidenote: yes, I am not creating a
Shellinterface with one implementation. Do you mind? Mockito is not. Bonus: you can now verify if the correct process was called: