Question: 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.

Answer: 
Tomasz Nurkiewicz has provided a wonderful answer. But there is an alternative.

You can move `Runtime.getRuntime()` into its own method in the class that you're testing; then test it with a Mockito spy. In the Mockito spy, you can override this particular method with a version that creates a mock `Runtime`, then do whatever you like with it.

However, Tomasz's approach is cleaner, and I recommend it thoroughly. I just wanted to say that there IS another way to do it, without resorting to Powermock.
I used sub-classing and overriding (as explained in Michael C. Feathers' book "Working effectively with legacy code")

Suppose we have the following code:

```
public class CannotChange{
    public CannotChange(){}
    public void TryingToTest() throws IOException {
        ...
        Process proc = Runtime.getRuntime().exec("command");
        ...
    }
}

```

Extract the runtime code into a protected method:

```
    public void TryingToTest() throws IOException {
        ...
        Process proc = execute("command");
        ...
    }
    protected Process execute(string command){ 
        return Runtime.getRuntime().exec(command);
    }     

```

Then in the test class create a class that extends CannotChange class and overrides 
execute

```
public class CannotChangeTest{

   private class CannotChangeForTesting extends CannotChange{
      public CannotChangeForTesting(){ super(); }
      @Override
      public Process execute(String command){
         return null; //or a mocked object
      }
   }

   @Test
   public void TestMethod(){
      CannotChange cannotChange = new ForTestCannotChange();
      cannotChange.TryingToTest();
   }
}

```