RequestReader { public String readRequest() throws IOException { StringBuffer buffer = new StringBuffer(); int intC = reader.read(); while (intC != -1) { char c = (char) intC; if (c == '\n') { break; } if (buffer.length() >= maxLineLength) { throw new IOException("input too long"); } buffer.append(c); intC = reader.read(); } return buffer.toString(); } RequestReader(BufferedReader reader); String readRequest();  }
@Test public void readsFromBufferedReader() throws IOException { String requestString = "test request string"; setupReadLine(requestString); String actualRequestString = testSubject.readRequest(); Assert.assertEquals(actualRequestString, requestString); }
@Test public void limitsInputLength() throws IOException { OngoingStubbing bufferedReaderStubbing = when(bufferedReaderMock.read()); for (int i = 0; i < 300; i++) { bufferedReaderStubbing = bufferedReaderStubbing.thenReturn(42); } try { testSubject.readRequest(); Assert.fail("Should have thrown exception"); } catch (IOException e) { Assert.assertEquals(e.getMessage(), "input too long"); } }