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

[SUREFIRE-1556] fail fast on empty element names #11

Merged
merged 4 commits into from
Apr 21, 2021
Merged

[SUREFIRE-1556] fail fast on empty element names #11

merged 4 commits into from
Apr 21, 2021

Conversation

elharo
Copy link
Contributor

@elharo elharo commented Feb 23, 2020

@Tibor17 Likely not a full fix, but it should help locate the root cause.

public void after()
throws Exception

@Test
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the expected property in the annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Expected was a bad design now replaced in JUnit 4.13 with assertThrows. The problem is that @Expected captures unexpected exceptions thrown from different parts of the method body and thus hides test failures.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use ExpectedException which is the Rule in JUnit4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same issue. This is not a full replacement for the pattern below and risks false negatives. See https://github.com/junit-team/junit4/wiki/Exception-testing

Copy link
Contributor

Choose a reason for hiding this comment

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

@elharo
I am a practicle man. You can of course use [1] with anonymous class instead of Lambda but you can use a traditional ExpectedException with the old version of JUnit.
[1]: https://junit.org/junit4/javadoc/4.13/org/junit/Assert.html#assertThrows(java.lang.String,%20java.lang.Class,%20org.junit.function.ThrowingRunnable)

import org.junit.rules.ExpectedException;

import static org.hamcrest.Matchers.equalTo;

    @Rule
    public final ExpectedException e = ExpectedException.none();

    @Test
    public void testNoStartTag() throws IOException
    {
        e.expect( IllegalArgumentException.class );
        e.expectMessage( equalTo( "Element name cannot be empty" ) );
        writer.startElement( "" );
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That has the same problem. It does not guarantee the expected exception was thrown by the right method. JUnit has rightly backed away from this style of testing exceptions.

Copy link
Contributor

Choose a reason for hiding this comment

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

@elharo
In practice the last line with main code must be followed by e.expect. So yes there is guarantee. It is clear that only that one line has to throw the exception. If it does not, then the ExpectedException fails the build. That's why it was designed in JUnit4. You can use the new assertThrows but it has another meaning - wrapping the exception and checking a new errors or exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works and it follows the recommendation of the JUnit Wiki and the design of JUnit from 4.13 onward. I'd like to move ahead with fixing this bug. Can I get an approval for this?

@MartinKanters
Copy link

@elharo Is this PR still valid and needed? We're looking to release maven-shared-utils, and I'm wondering if we should include this.

@elharo
Copy link
Contributor Author

elharo commented Apr 16, 2021

Yes, I believe this PR is still valid and useful though it looks like I need to merge in master. Give me a minute.

Copy link
Contributor Author

@elharo elharo left a comment

Choose a reason for hiding this comment

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

PTAL

public void after()
throws Exception

@Test
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works and it follows the recommendation of the JUnit Wiki and the design of JUnit from 4.13 onward. I'd like to move ahead with fixing this bug. Can I get an approval for this?

@MartinKanters
Copy link

@elharo Hey, you got your approval, but we can't merge anymore due to conflicts. Can you please rebase on master and squash commits?

@elharo elharo merged commit 4e93c56 into master Apr 21, 2021
@elharo elharo deleted the xml3 branch April 21, 2021 11:46
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.

5 participants