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

Replace Assert.fail usage with AssertJ fluent testing #6029

Merged
merged 1 commit into from
Oct 21, 2022

Conversation

nastra
Copy link
Contributor

@nastra nastra commented Oct 21, 2022

This replaces a bunch of code of the form

try {
      // do something
      Assert.fail(...);
    } catch (Exception e) {
        // make sure exception has correct message/code/...
    }

There are still a few legit places left that use Assert.fail() so I didn't create a checkstyle rule to prevent usage

@@ -37,178 +34,110 @@ private CustomCheckedException(String message) {
@Test
public void testRunSafely() {
CustomCheckedException exc = new CustomCheckedException("test");
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to admit that the original test code was quite difficult to read, so I tried to simplify it to the bare minimum

@nastra nastra force-pushed the assert-fail-improvements branch from 0ba187a to ac040a6 Compare October 21, 2022 11:05
@nastra nastra force-pushed the assert-fail-improvements branch from ac040a6 to a06939c Compare October 21, 2022 11:08
Copy link
Contributor

@Fokko Fokko left a comment

Choose a reason for hiding this comment

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

I love it!

@nastra nastra requested a review from rdblue October 21, 2022 13:38
"Validation should complain about missing field",
e.getMessage().contains("Cannot find field 'missing' in struct:"));
}
Assertions.assertThatThrownBy(() -> unbound.bind(struct))
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, this was so old it predated adding the assertThrows methods!

"test finally suppression",
finallySuppressed.getMessage());
}
Exception suppressedOne = new Exception("test catch suppression");
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks a lot better.

Assertions.assertThatThrownBy(
() ->
glueCatalog.createNamespace(
Namespace.of("denied_" + UUID.randomUUID().toString().replace("-", ""))))
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future, it would be better to put the namespace in a variable to avoid such awkward auto formatting.


Namespace namespace = Namespace.of("allowed_" + UUID.randomUUID().toString().replace("-", ""));
try {
glueCatalog.createNamespace(namespace);
} catch (GlueException e) {
LOG.error("fail to create or delete Glue database", e);
Assert.fail("create namespace should succeed");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the idea here that the whole test will fail so we don't need to assert?

Assert.assertEquals(message + ", http code", httpCode, e.getHttpCode());
Assert.assertEquals(message + ", error code", errorCode, e.getErrorCode());
}
Assertions.assertThatThrownBy(task::run)
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems more complicated than the original try/catch. Not sure it's worth it.

@@ -94,7 +95,7 @@ public void testWriteOption() throws Exception {
writer.add(record2);
}

Class clazzFileDump = Class.forName("org.apache.orc.tools.FileDump");
Class<?> clazzFileDump = Class.forName("org.apache.orc.tools.FileDump");
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding these! Never good to have types without parameters.

@rdblue rdblue merged commit 39a2c12 into apache:master Oct 21, 2022
@rdblue
Copy link
Contributor

rdblue commented Oct 21, 2022

Thanks, @nastra!

@nastra nastra deleted the assert-fail-improvements branch October 21, 2022 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants