-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use a non-deprecated assertThat, and change several test assertions to use assertThat #14518
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
base: main
Are you sure you want to change the base?
Conversation
Arrays.stream(c.destDir.listAll()) | ||
.collect(Collectors.toSet()) | ||
.containsAll(merge.getMergeInfo().files())); | ||
assertFalse(Set.of(c.destDir.listAll()).containsAll(merge.getMergeInfo().files())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't easily translate into a matcher, so I've changed to use Set.of
to make it a bit more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the previous version was actually more intuitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so - using a stream from an array to collect into a set, vs just creating a set directly from the array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, yes, it's all subjective. The previous version had stream methods over multiple lines - to me that read easier compared to the one-liner now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on cleaning up tests. I still favor assertj assertions over hamcrest - they seem so much cleaner to me. But let's keep things consistent, no worries. This looks good to me.
I'll merge this one later today if there are no objections. |
@@ -233,7 +245,7 @@ public void testPositionIncrementMultiFields() throws Exception { | |||
PhraseQuery query = new PhraseQuery("indexed_not_tokenized", "test1", "test2"); | |||
|
|||
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs; | |||
assertEquals(1, hits.length); | |||
assertThat(hits, arrayWithSize(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering why this is considered an improvement, the previous assert is much more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think error messages are better... I haven't been using hamcrest much so I can't tell for sure - assertj definitely provides more informative error messages. I don't mind either way, even though hamcrest's style is confusing to me too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm confused what could be better about the existing error message, it asserts that array's length is 1, and if its something else, you'll see that number.
So... zero benefit, but at the cost of making the code significantly more difficult to read (which is actually very important when trying to debug the tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the more declarative style of assertThat
, and the assertion failure specifies what the problem actually is, rather than 'this singular value is not what is expected'
I have taken another look. The one thing I would revert is replacing assertEquals with hamcrest matchers - it is indeed the same functionality, the same output message and more verbose code. Other changes seem more palatable to me - like array containment cleaned up (and this should result in a better error message, should something wrong happen), assertTrue conditions replaced with equality checks (so that values are reported, not just failed assertion). |
assertThat(doc.getFields(), hasSize(10)); | ||
doc.removeFields("keyword"); | ||
assertEquals(8, doc.getFields().size()); | ||
assertThat(doc.getFields(), hasSize(8)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of changes that don't provide any extra value, I think.
@@ -207,7 +219,7 @@ public void testGetValuesForIndexedDocument() throws Exception { | |||
|
|||
// ensure that queries return expected results without DateFilter first | |||
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs; | |||
assertEquals(1, hits.length); | |||
assertThat(hits, arrayWithSize(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. It's more verbose and does the same thing.
Set<String> seen = new HashSet<>(); | ||
StoredFields storedFields = searcher.storedFields(); | ||
for (int i = 0; i < 3; i++) { | ||
Document doc2 = storedFields.document(hits[i].doc); | ||
Field f = (Field) doc2.getField("id"); | ||
if (f.stringValue().equals("id1")) result |= 1; | ||
else if (f.stringValue().equals("id2")) result |= 2; | ||
else if (f.stringValue().equals("id3")) result |= 4; | ||
else fail("unexpected id field"); | ||
switch (f.stringValue()) { | ||
case "id1", "id2", "id3" -> seen.add(f.stringValue()); | ||
default -> fail("unexpected id field"); | ||
} | ||
} | ||
writer.close(); | ||
reader.close(); | ||
dir.close(); | ||
assertEquals("did not see all IDs", 7, result); | ||
assertThat("did not see all IDs", seen, containsInAnyOrder("id1", "id2", "id3")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be just assertEquals(seen, Set.of(...)) - this would detect anything extra and check for the occurrence of all ids?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
containsInAnyOrder
fails if there are any extra items in the collection.
More generally, assertEquals(..., Set.of())
checks if the value is a Set
, which is not actually important here. The collection could be a List
and the test would still work. So checking for Set
isn't required here for the test to pass, whereas containsInAnyOrder
doesn't care about the type of collection used.
Arrays.stream(c.destDir.listAll()) | ||
.collect(Collectors.toSet()) | ||
.containsAll(merge.getMergeInfo().files())); | ||
assertFalse(Set.of(c.destDir.listAll()).containsAll(merge.getMergeInfo().files())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the previous version was actually more intuitive?
assertThat(parseInt("1"), equalTo(1)); | ||
assertThat(parseInt("-10000"), equalTo(-10000)); | ||
assertThat(parseInt("1923"), equalTo(1923)); | ||
assertThat(parseInt("-1"), equalTo(-1)); | ||
assertThat(ArrayUtil.parseInt("foo 1923 bar".toCharArray(), 4, 4), equalTo(1923)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of a potentially better message but what's wrong with assertEquals(a, b)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertEquals
is ambiguous to what the thing being test is vs the expected value it should have, assertThat
is not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, true, if you're using it the first time perhaps. :)
The focus of this PR was to change I prefer the |
I'm fine with merging this change. I don't mind if the code becomes a bit more verbose because of it. Rob? |
my concern wasn't about verbosity: if you are concerned about this, steer clear of java! instead just keeping "barrier to entry" to understanding the tests low. This includes requiring developer to go and understand a third-party library in order to debug it. But if this is really more natural to most developers, then I'm ok. |
The main reason for this PR is using an un-deprecated
assertThat
by default. I've also updated a few uses of old-style assertions to useassertThat
andMatchers
for readability and a more descriptive error message. There's obviously loads more that could be updated; this is a few to start with.