Mocking a

66 views
Skip to first unread message

mikael petterson

unread,
Jan 21, 2022, 7:07:01 AMJan 21
to mockito
Hi,

I have the following in a class called MyClass:

public class MyClass extends Base{

 
    @Override
    public StringBuilder readUntilFound() {

        InputStream is = super.getInputStream();
        return decode(is);

    }

    StringBuilder decode(InputStream is) {
        final StringBuilder buffer = new StringBuilder(12096);
        int character = 1;

        try (BufferedInputStream bis = new BufferedInputStream(is)) {
            while (true) {
                character = bis.read();
                //Checking if we have a end of stream or  netconf message
                //check for EOF
                if (character == -1) {
                    break;
                } else if (NetconfUtil.isEndOfChunkedMessageReached(buffer, character, bis)) {
                    break;
                }

            }
        }catch (InterruptedIOException iioe) {
                Thread.currentThread().interrupt();
       
        } catch (IOException e) {
          
        }

        return buffer;
    }
}

I need to test that what happens when  a InterruptedIOExceptionis thrown by:

java.io.InterruptedIOException: null at com.trilead.ssh2.channel.ChannelManager.getChannelData(ChannelManager.java:937) ~[trilead-ssh2-build-217-jenkins-27.jar:?] at com.trilead.ssh2.channel.ChannelInputStream.read(ChannelInputStream.java:58) ~[trilead-ssh2-build-217-jenkins-27.jar:?] at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[?:1.8.0_191] at java.io.BufferedInputStream.read(BufferedInputStream.java:265) ~[?:1.8.0_191]

So I took the following approach to generate this exception in a test

@Test
public void testInterrupedIOException() throws Exception {
     
             InputStream inputStreamMock = mock(InputStream.class);
        BufferedInputStream bufferedInputStreamMock = mock(BufferedInputStream.class, withSettings().useConstructor(inputStreamMock));
        when(bufferedInputStreamMock.read()).thenThrow(new InterruptedIOException())   
        new MyClass().decode(bufferedInputStreamMock);
      
    }

However I don't see any InterruptedIOException thrown.

Any ideas or hints?

//mike

Mirko Alicastro

unread,
Jan 23, 2022, 12:12:40 PMJan 23
to mockito
Hey Mike. You could use mockConstruction as below.

@Test
public void testInterruptedIOException() {
  InputStream inputStream = mock(InputStream.class);

  try (MockedConstruction<BufferedInputStream> mockedConstruction = mockConstruction(BufferedInputStream.class,
    (mock, context) -> {
    if (!singletonList(inputStream).equals(context.arguments()))
      fail("BufferedInputStream constructed with unexpected argument");
    when(mock.read()).thenThrow(new InterruptedIOException());
  })) {
    new MyClass().decode(inputStream);

    assertThat(mockedConstruction.constructed()).hasSize(1);
    // add your assertions 
  }
}
Reply all
Reply to author
Forward
0 new messages