-
Notifications
You must be signed in to change notification settings - Fork 174
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
Make asserts meaningful #232
base: master
Are you sure you want to change the base?
Conversation
@@ -103,7 +103,7 @@ public void testChangeLogWithBadUserDateFormat() throws Exception { | |||
|
|||
fail("mojo execution must fail."); | |||
} catch (MojoExecutionException e) { | |||
assertTrue(true); | |||
assertNotNull(e.getMessage()); |
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 guessing that the true meaning is that mojo.execute()
throws. And IMO it'd better to assert that that happens. The non null message seems to be secondary.
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.
Yes, it's secondary but still not great. The real test is the fail on the previous lines.
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'd rather see fail
there replaced with assertThrows
. This would be meaningful.
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.
It means no more or less than this code does. If you want to send that PR, I'll approve it. Otherwise, let's proceed with this change.
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.
Proposed change introduces new constraint for the exception message. It wasn't there, wasn't needed then, and isn't needed now.
I think the always true assertion was added to satisfy checkstyle to avoid empty block.
If this PR gets merged, refactoring to come next, would need to consider that. Not only that the exception is thrown (what is currently checked) but also that the message is not null.
IMO, it's unnecessary and it would be better to migrate to assertThrows
only.
assertTrue(true)
could then be eliminated.
assertNotNull(...)
couldn't.
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.
putting in meaningless lines of code to make checkstyle happy is not a good thing.
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 using @Test(expected = MojoExecutionException.class)
would be better choice.
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.
There is no @Test(expected ...)
in JUnit 5
For me assertThrows
is the best we control which one action generate exception
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.
There is no
@Test(expected ...)
in JUnit 5
Lines 281 to 285 in ad63ac5
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.13.2</version> | |
</dependency> |
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 know that now ju 4 is used ... 😄
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.
As @pzygielo has pointed out in comments:
The JUnit 4 way to test for an expected MojoExecutionException
is
@Test(expected = MojoExecutionException.class)
instead of a try/catch block
(non binding review)
That's not good and the JUnit project no longer recommends it. The problem is it doesn't indicate which line of code threw the exception so you can get false positives — tests that pass when they shouldn't — when the same exception class gets thrown from somewhere unexpected. |
Indeed. |
The code line And I know that the way to test assertions in JUnit Jupiter is different. |
No, that's done with the try block. |
Asserts no longer guarantted to pass.