Last active
July 18, 2019 13:49
-
-
Save rajatvig/a907674b11e12a969a5a to your computer and use it in GitHub Desktop.
InputStream Matcher for Mockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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