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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.