Skip to content

Instantly share code, notes, and snippets.

@rajatvig
Last active July 18, 2019 13:49
Show Gist options
  • Save rajatvig/a907674b11e12a969a5a to your computer and use it in GitHub Desktop.
Save rajatvig/a907674b11e12a969a5a to your computer and use it in GitHub Desktop.
InputStream Matcher for Mockito
package test.support;
import com.google.common.io.CharStreams;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.mockito.Matchers.argThat;
@AllArgsConstructor
public class InputStreamMatcher extends BaseMatcher<InputStream> {
private final String expectedData;
@SneakyThrows(IOException.class)
@Override public boolean matches(Object item) {
InputStream inputStream = (InputStream) item;
String actual = CharStreams.toString(new InputStreamReader(inputStream));
inputStream.reset();
return actual.equals(expectedData);
}
@Override public void describeTo(Description description) {
description.appendText("InputStream containing ").appendValue(expectedData);
}
public static InputStream matchesContent(String expected) {
return argThat(new InputStreamMatcher(expected));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment