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

Make asserts meaningful #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Make asserts meaningful #232

wants to merge 1 commit into from

Conversation

elharo
Copy link
Contributor

@elharo elharo commented Dec 12, 2024

Asserts no longer guarantted to pass.

@@ -103,7 +103,7 @@ public void testChangeLogWithBadUserDateFormat() throws Exception {

fail("mojo execution must fail.");
} catch (MojoExecutionException e) {
assertTrue(true);
assertNotNull(e.getMessage());

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.

Copy link
Contributor Author

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.

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.

Copy link
Contributor Author

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.

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.

Copy link
Contributor Author

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.

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.

Copy link
Member

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

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

maven-scm/pom.xml

Lines 281 to 285 in ad63ac5

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

Copy link
Member

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 ... 😄

@elharo elharo marked this pull request as ready for review December 12, 2024 14:02
Copy link

@Bukama Bukama left a 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)

@elharo
Copy link
Contributor Author

elharo commented Dec 25, 2024

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.

@pzygielo
Copy link

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.

@Bukama
Copy link

Bukama commented Dec 26, 2024

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.

The code line assertNotNull(e.getMessage()); does not check for a specific line of code either. You should then check for a specific exception message.

And I know that the way to test assertions in JUnit Jupiter is different.

@elharo
Copy link
Contributor Author

elharo commented Dec 26, 2024

The code line assertNotNull(e.getMessage()); does not check for a specific line of code either. You should then check for a specific exception message.

No, that's done with the try block.

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.

4 participants