Skip to content
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

test: Migrate ImportTest to JUnit5 #4225

Merged
merged 2 commits into from
Oct 16, 2021
Merged

test: Migrate ImportTest to JUnit5 #4225

merged 2 commits into from
Oct 16, 2021

Conversation

spencerwi
Copy link
Contributor

@spencerwi spencerwi commented Oct 14, 2021

#3919

Another test class migration. This one had several cases where the JUnit assert<Something>(...) signature changed, so I took the opportunity to swap to assertThat(...) using already-available Hamcrest core matchers, which should help shield against any future signature changes (since Hamcrest is a pretty stable assertions API, or at least a widely-used one).

@@ -202,7 +196,7 @@ public void testImportOfAnInnerClassInAClassPackage() throws Exception {
final CtMethod<?> methodVisit = client.getMethodsByName("visit").get(0);

final CtType<Object> innerClass = factory.Type().get("spoon.test.imports.testclasses.DefaultClientClass$InnerClass");
assertEquals("Type of the method must to be InnerClass accessed via DefaultClientClass.", innerClass, methodVisit.getType().getDeclaration());
assertEquals(innerClass, methodVisit.getType().getDeclaration(), "Type of the method must to be InnerClass accessed via DefaultClientClass.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were lots of these cases where assertEquals(message, expected, actual) is now assertEquals(expected, actual, message). Pretty much all of them were fixed by simply permuting arguments.

@@ -453,9 +447,9 @@ void checkCanAccess(String aClassName, boolean isInterface, boolean canAccessCli
CtTypeReference<?> accessType;

if(canAccessClientClass) {
assertTrue("ClientClass should have access to "+aClassName+" but it has not", aClientClass.canAccess(target));
assertTrue(aClientClass.canAccess(target), "ClientClass should have access to "+aClassName+" but it does not have access");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, assertTrue(message, subject) and assertFals(message, subject) are now assertTrue(subject, message) and assertFalse(subject, message).

There were a couple of cases in here where I also clarified the language on the message a bit to be more idiomatically English.

@@ -663,7 +657,7 @@ public void testDeepNestedStaticPathWithTypedParameter() {
fail(e.getMessage());
}
CtClass<?> mm = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.StaticWithNested");
assertTrue("new spoon.test.imports.testclasses2.StaticWithNested.StaticNested.StaticNested2<K>();", mm.toString().contains("new spoon.test.imports.testclasses2.StaticWithNested.StaticNested.StaticNested2<K>();"));
assertThat(mm.toString(), containsString("new spoon.test.imports.testclasses2.StaticWithNested.StaticNested.StaticNested2<K>();"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then there were many of these cases: assertTrue(someString.contains(someOtherString) or assertFalse(!someString.contains(someOtherString)). I swapped these to assertThat(someString, containsString(someOtherString)) and assertThat(someString, CoreMatchers.not(containsString(someOtherString))), which yields a more-descriptive message when assertions fail.

Copy link
Collaborator

@monperrus monperrus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @spencerwi Will merge.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have a tiny request on a style change.

assertThat(
"The file should not contain a static import to the inner enum method values",
output,
CoreMatchers.not(containsString("import static spoon.test.imports.testclasses.StaticImportsFromEnum$DataElement.values;"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the assertions more readable, could you use static imports instead? I.e. such that this looks like so instead:

Suggested change
CoreMatchers.not(containsString("import static spoon.test.imports.testclasses.StaticImportsFromEnum$DataElement.values;"))
not(containsString("import static spoon.test.imports.testclasses.StaticImportsFromEnum$DataElement.values;"))

Instead of importing CoreMatchers, you do import static org.hamcrest.CoreMatchers.not. It's the most common way of using the matchers, and will match the style of other test classes. It's also IMO the most readable.

Copy link
Contributor Author

@spencerwi spencerwi Oct 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can do! I went back and forth on that one, to be honest; not(...) is more readable than CoreMatchers.not(...), but I also know that it's a common-enough method name to be likely to have ambiguous namespace resolution when just reading the code without an IDE's help, so I wasn't sure which way to go with it.

I'll make the change here and puth that up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change applied. Thanks for the review!

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @spencerwi

@slarse slarse merged commit 62c22cd into INRIA:master Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants