-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
fix: Test failure in TestModule.testModuleNames() (#4285) #4286
Conversation
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.
Hi @xzel23, thanks for fixing the flaky test! I propose we go one step further to improve it while we're at it, see comment :)
List<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toList()); | ||
assertTrue(list.contains("foo")); | ||
assertTrue(list.contains("bar")); |
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.
While you're poking about here anyway, how about improving it just a little bit more? This test is a bit weak (because it leaves the possibility of there being other module names in the list), and it also isn't entirely clear about its purpose.
I propose we assert on the entire list. And instead of a list use a set, because we care only about distinct module names.
List<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toList()); | |
assertTrue(list.contains("foo")); | |
assertTrue(list.contains("bar")); | |
Set<String> moduleNames = Arrays.stream(cu) | |
.map(CompilationUnit::getModuleName) | |
.map(String::valueOf) | |
.collect(Collectors.toSet()); | |
assertThat(moduleNames, equalTo(Set.of("bar", "foo"))); |
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.
done. I replaced the suggested deprecated assertThat with a non-deprecated one.
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.
Hmm, I didn't suggest the deprecated assertThat
, I just didn't specify which one to use (in fact I didn't even remember that JUnit 4 had a built-in assertThat
). You've imported both, so I suspect your IDE auto-imported a bunch of stuff. See review comments.
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.MatcherAssert; |
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.
Not needed
import org.hamcrest.CoreMatchers; | |
import org.hamcrest.MatcherAssert; |
@@ -54,11 +56,16 @@ | |||
import java.util.Collections; | |||
import java.util.ArrayList; |
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 ArrayList
is needed anymore.
import java.util.ArrayList; |
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertThat; |
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.
import static org.junit.Assert.assertThat; |
assertTrue(list.contains("foo")); | ||
assertTrue(list.contains("bar")); | ||
Set<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toSet()); | ||
MatcherAssert.assertThat(list, is(Set.of("foo", "bar"))); |
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.
MatcherAssert.assertThat(list, is(Set.of("foo", "bar"))); | |
assertThat(list, is(Set.of("foo", "bar"))); |
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.
My IDE had somehow lost track of the imports and marked everything as missing, so I didn't see the unused exports. Fixed after restart. I have cleaned it up and added a static import for the hamcrest assertThat as suggested by the junit deprecationcomment. Hope it's alright 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.
Yeah, I feel you. I find that most IDEs with default settings are over-eager when it comes to auto-importing stuff, it gets rather confusing rather quickly.
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.
Thanks @xzel23, this is a much more robust (and non-flaky) test!
This fixes #4285. The test relied on the order of entries returned by
File#listFiles()
, which is unspecified (thank you @SirYwell ).