Skip to content

Conversation

@elharo
Copy link
Contributor

@elharo elharo commented Dec 12, 2024

Asserts no longer guaranteed to pass.

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
Contributor

@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
Contributor

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.

@elharo elharo merged commit 1487251 into master Jan 9, 2025
37 checks passed
@elharo elharo deleted the true branch January 9, 2025 20:53
@kwin kwin added enhancement New feature or request maintenance and removed enhancement New feature or request labels Aug 6, 2025
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.

5 participants