Adding a New Test

With our initial ugly test whittled into two sleek, clear tests, it’s now relatively easy to add a couple of new tests. First let’s write a test that demonstrates how a search returns true for the errored() query:

iloveyouboss/test-10/test/util/SearchTest.java
 
@Test
 
public​ ​void​ returnsErroredWhenUnableToReadStream() {
 
stream = createStreamThrowingErrorWhenRead();
 
Search search = ​new​ Search(stream, ​""​, ​""​);
 
 
search.execute();
 
 
assertTrue(search.errored());
 
}
 
 
private​ ​InputStream​ createStreamThrowingErrorWhenRead() {
 
return​ ​new​ ​InputStream​() {
 
@Override
 
public​ ​int​ read() ​throws​ IOException {
 
throw​ ​new​ IOException();
 
}
 
};
 
}

And add a test for the opposite:

iloveyouboss/test-10/test/util/SearchTest.java ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.