99

Please provide pointers to help me mock that java InputStream object. This is the line of code that I would wish to Mock:

InputStreamReader inputData = new InputStreamReader(System.in);
bufferdReader = new BufferedReader(inputData);
bufferdReader.readLine(); 
1
  • 1
    Is your problem the System.in ? You probably want to refactor your code so that another InputStream is passed instead of System.in ; then you can use any Mocking framework (like mentionned in the answers) to mock this InputStream.
    – phtrivier
    Commented Jun 16, 2011 at 12:09

11 Answers 11

135

You could use commons-io to create some stub input streams:

InputStream stubInputStream = 
     IOUtils.toInputStream("some test data for my input stream", "UTF-8");
5
  • 1
    If you want to use the stream more than once, how would you reset it after use?
    – exic
    Commented Nov 21, 2013 at 9:39
  • You could wrap it in a BufferedInputStream and use mark() and reset(). But probably easier to just instantiate a new one in your setup method (assuming we're still talking about testing here).
    – Eric
    Commented Dec 9, 2013 at 15:43
  • 1
    since v2.3 you need to add the Charset, for ex: toInputStream("someString", "UTF-8"). The version with only String as parameters is deprecated.
    – JonyD
    Commented Feb 13, 2017 at 11:20
  • Thanks @JonyD! I updated the answer to include the charset parameter.
    – Eric
    Commented Feb 21, 2017 at 19:44
  • 1
    My favorite answer! I would just add that instead of using the non type safe string, you could be using StandardCharsets.UTF_8 Commented Aug 2, 2019 at 20:18
133

You could just use a ByteArrayInputStream and fill it with your test data.

@Brad's example from the comments:

InputStream anyInputStream = new ByteArrayInputStream("test data".getBytes());
3
  • Additional benefit is that ByteArrayInputStream supports mark() and reset()
    – Hank
    Commented Apr 17, 2015 at 15:39
  • 5
    Nice and simple ... InputStream anyInputStream = new ByteArrayInputStream("test data".getBytes());
    – Brad
    Commented May 12, 2015 at 12:14
  • 2
    in Kotlin: val inputStream = "test data".byteInputStream()
    – swooby
    Commented Feb 17, 2021 at 20:59
66
BufferedReader bufferedReader = org.mockito.Mockito.mock(BufferedReader.class);
when(bufferedReader.readLine())
  .thenReturn("first line")
  .thenReturn("second line");

org.junit.Assert.when(new Client(bufferedReader).parseLine())
  .thenEquals(IsEqual.equalTo("first line"));
2
  • How do I import Client? I'm sorry but I'm new to mockito and junit, you are using Client on this line: org.junit.Assert.when(new Client(bufferedReader).parseLine()) Commented Dec 5, 2019 at 20:17
  • Client is a class that uses the buffered reader to read line by line and parse it Commented Dec 5, 2019 at 20:49
21

I disagree with the selected answer for this question. Mocking frameworks like Mockito are nice and all, however when standard java api is available you might consider using that instead.

i.e.

BufferedReader reader = new BufferedReader(new StringReader("some string"));

Why use a Mock object in your test classes when you could use a real one with all its state and behaviour?

To see more about how this works, you could look up the 'decorator' design pattern.

2
  • 5
    Here's a usecase: mock multiple different invocations, e.g. in a loop (wrong input1 -> wrong input2 -> correct input3).
    – Jan Groth
    Commented Mar 21, 2014 at 12:08
  • Keep your InputStream code and your Reader code in separate classes, just like the java api does. Have your test class pass an implementation of Reader, not InputStream, and in production code, also pass an implementation of Reader to the class that is reading your data / doing your data processing. Commented Mar 27, 2016 at 9:10
4
@Test
    public void testReadFile() {
    TestClass ClassName = Mockito.mock(TestClass.class);
     InputStream in = Mockito.mock(InputStream.class);
     InputStreamReader inr =Mockito.mock(InputStreamReader.class);
     BufferedReader bufferedReader =Mockito.mock(BufferedReader.class);
       try {
         PowerMockito.whenNew(InputStreamReader.class).withArguments(in).thenReturn(inr);
         PowerMockito.whenNew(BufferedReader.class).withArguments(inr).thenReturn(bufferedReader);
         String line1 = "example line";
         PowerMockito.when(bufferedReader.readLine()).thenReturn(line1).thenReturn(null);
         method return type = Whitebox.invokeMethod(ClassName, "MethodName", arguement);
         assertEquals("result is::","expected", actual);
     } catch (Exception e) {
         e.printStackTrace();
     }
 }
3

Change your object so it is easier to test, something like this:

public MyObject {
    private InputStream inputStream;

    public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}

    public void whatever() {
        InputStreamReader inputData = new InputStreamReader(inputStream);
        bufferdReader = new BufferedReader(inputData);
        bufferdReader.readLine(); 
    }
}

then when you use your object initialize its inputStream first:

MyObject myObject = new MyObject();
myObject.setInputStream(System.in);

Now you have an object where you can test it using any implementation of InputStream you want (ByteArrayInputStream is a good one to try).

3
String testString = "test\nstring";
InputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));

BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

Assert.assertEquals("test", reader.readLine());
Assert.assertEquals("string", reader.readLine());
1

The best solution i found is use

final InputStream inputStream1 = IOUtils.toInputStream("yourdata");

and then wrap the inpustream in bufferedReader best way to write test around input Stream

1
  • 1
    This adds another dependency on IOUtils - which makes your test code larger. Not the desired effect we want from small unit tests... Commented Mar 22, 2016 at 14:54
0

Assuming you are using Maven you can put a resource into "src/test/resources/" folder let's say "src/test/resources/wonderful-mock-data.xml". Then in you jUnit your can do:

    String resourceInputFile = "/database-insert-test.xml";
    URL url = this.getClass().getResource(resourceInputFile);
    Assert.assertNotNull("Can't find resource " + resourceInputFile, url);

    InputStream inputStream = url.openStream();

    // Now you can just use the inputStream for method calls requiring this param
    (...)

In this example the url varialble will be null if the given resource can't be found inside current classpath. This approach allows you to put multiple scenarios inside different resourceInputFile(s)... Also remember that all kind of resources under "src/test/resources/" (not just xml files, any kind like txt, html, jpeg, etc.) are normaly available as classpath resources from all jUnit tests.

0
when(imageService.saveOrUpdate(Matchers.<Image>anyObject())).thenReturn(image);

Reference http://www.javased.com/?api=org.mockito.Matchers

0

You can also mock InpuStream use this guide: https://www.baeldung.com/java-mocking-inputstream

For example:

    @Test
    public void givenSimpleImplementation_shouldProcessInputStream() throws IOException {
        int byteCount = processInputStream(new InputStream() {
            private final byte[] msg = "Hello World".getBytes();
            private int index = 0;
            @Override
            public int read() {
                if (index >= msg.length) {
                    return -1;
                }
                return msg[index++];
            }
        });
        assertThat(byteCount).isEqualTo(11);
    }

    int processInputStream(InputStream inputStream) throws IOException {
        int count = 0;
        while(inputStream.read() != -1) {
            count++;
        }
        return count;
    }
1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented May 25, 2023 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.