-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hamcrest Matcher for Iterable comparison #23
Comments
…Move TestDoubles and FailAnswer to packages. #23
…Move TestDoubles and FailAnswer to packages. #23
…Move TestDoubles and FailAnswer to packages. #23
What's an actual use case for this that doesn't work with |
Nevermind, I just noticed that you "use" it in #25. I think that doesn't really justify this class, because the premise is wrong (see my comment in #25). Regarding assertTrue(dummy(Foo.class, "x").equals(dummy(Foo.class, "x")));
assertTrue(dummy(Foo.class, "x").equals(dummy(Foo.class, "y"))); Unfortunately that doesn't work this way, because you can not mock this this: public static <T> T namedDummy(Class<T> clazz, String o)
{
T result = mock(clazz, withSettings().defaultAnswer(new FailAnswer()).name("org.dmfs.MOCK:" + clazz.getCanonicalName() + ":" + o));
doReturn(clazz.toString()).when(result).toString();
return result;
}
public final static class MockNameMatcher extends BaseMatcher
{
private final MockingDetails mMockingDetails;
public static MockNameMatcher isMock(Object object)
{
return new MockNameMatcher(mockingDetails(object));
}
private MockNameMatcher(MockingDetails mockingDetails)
{
mMockingDetails = mockingDetails;
}
@Override
public boolean matches(Object item)
{
MockingDetails mockingDetails = mockingDetails(item);
return mockingDetails.isMock() && mockingDetails.getMockCreationSettings()
.getMockName().toString()
.equals(mMockingDetails.getMockCreationSettings().getMockName().toString());
}
@Override
public void describeTo(Description description)
{
}
} which would be used like this
There may be better ways to achieve this. the example in #25 would look like this: assertThat(
new MappedRowSetBatch<>(
new TestRowSet<Contract>(
namedDummy(RowSnapshot.class, "a"),
namedDummy(RowSnapshot.class, "b"),
namedDummy(RowSnapshot.class, "c")),
new MockFunction<>(
new Pair(namedDummy(RowSnapshot.class, "a"), namedDummy(Operation.class, "x")),
new Pair(namedDummy(RowSnapshot.class, "b"), namedDummy(Operation.class, "y")),
new Pair(namedDummy(RowSnapshot.class, "c"), namedDummy(Operation.class, "z"))),
contains(
isMock(namedDummy(Operation.class, "x"),
isMock(namedDummy(Operation.class, "y"),
isMock(namedDummy(Operation.class, "z"))); |
Interesting, nice idea to have value object for any type easily with those named dummies. Although, I am not sure that this Back to these What do you think? How about having something like |
Thinking about it a bit more, I can see now that you don't really want to create value objects with these named mocks but, in fact, find a solution for not needing to keep the references to dummies. |
I've considered a map or list to return the same instance for the same names/numbers. But that didn't appear like a clean solution to me. I felt this solution is a slightly cleaner way (although still not perfect). I don't see For brevity we could use numbered dummies (like you use it in The test above would look like this: assertThat(
new MappedRowSetBatch<>(
new MockRowSet<Contract>(
rowSnapthotDummy(1),
rowSnapthotDummy(2),
rowSnapthotDummy(3)),
new MockFunction<>(
new Pair(isDummy(rowSnapthotDummy(1)), operationDummy(1)),
new Pair(isDummy(rowSnapthotDummy(2)), operationDummy(2)),
new Pair(isDummy(rowSnapthotDummy(3)), operationDummy(3)))),
contains(
isDummy(operationDummy(1)),
isDummy(operationDummy(2)),
isDummy(operationDummy(3)))); |
In order to assert
Iterable
s easily, as a developer, I want aIterable
hamcrest Matcher.Hamcrest's IsIterableContainingInOrder doesn't have a factory method with
Iterable
parameter unfortunately.The text was updated successfully, but these errors were encountered: