PowerMock is a great tool that I have recently started using for testing some static methods. Unfortunately, I am NOT able to re-write anything (apart from the tests), and need PowerMock to be able to test this code strictly as-is.
This is my PowerMock test:
import java.io.*;
import org.junit.*;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest; 
@RunWith(MockitoJUnitRunner.class)
@PrepareForTest({Solution.class})
public class SolutionTest {
    // stream to record the output (System.out)
    private ByteArrayOutputStream testOutput;
    @Before
    public void setUpOutputStream() {
        testOutput = new ByteArrayOutputStream();
        System.setOut(new PrintStream(testOutput));
    }
    // input feed to Scanner (System.in)
    private void setInput(String input) {
        System.setIn(new ByteArrayInputStream(input.getBytes()));
    }
    @Test
    public void test1() {
        // set System.in
        setInput("foo");
        final String expected = "foobar";
        final String actual = testOutput.toString();
        // run the program (empty arguments array)
        Solution.main(new String[0]);
        assertEquals(expected, actual);
    }
    @Test
    public void test2() {
        setInput("new");
        Solution.main(new String[0]);
        final String expected = "newbar";
        final String actual = testOutput.toString();
        assertEquals(expected, actual);
    }
}
PowerMock has made it possible for me run (and pass) two tests in succession on a static method in a scenario such as this:
import java.util.Scanner;
public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        scanner.close();
        System.out.print(input + "bar");
    }
}
Before PowerMock, I had been stymied by the exception (caused by having to test static method) java.lang.IllegalStateException: Scanner closed
However, in this alternate scenario, which calls a second static method (also scanner is a static member), that issue re-emerges.
import java.util.Scanner;
public class Solution {
    static void printString(String s) {
        System.out.print(s);
    }
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        String input = scanner.nextLine();
        printString(input + "bar");
        scanner.close();
    }
}
Here, test1 will pass, but test2 can't even run because of
java.lang.IllegalStateException: Scanner closed
I need both tests to pass in the latter scenario, as they do in the former.
For your convenience (and because a tested answer will be most valuable), my dependencies are as follows:
<dependencies>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Thanks very much!
 
     
    