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

Made ExpectedException.isAnyExceptionExpected public #1443

Merged
merged 2 commits into from
May 6, 2017

Conversation

jokaah
Copy link
Contributor

@jokaah jokaah commented Apr 27, 2017

Is there any harm in making this method public?

I have a base class for our test classes that has both an ExpectedException rule and a Verifier rule. The problem is that I don't want to run the Verifier if any exception is expected. So this is my current solution:

public abstract class TestBase {
    private Method isAnyExceptionExpected;

    public TestBase() throws Exception {
        this.isAnyExceptionExpected = ExpectedException.class.getDeclaredMethod("isAnyExceptionExpected");
        this.isAnyExceptionExpected.setAccessible(true);
    }

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Rule
    public Verifier verifier = new Verifier() {
        @Override
        public void verify() {
            try {
                if (!(boolean) isAnyExceptionExpected.invoke(thrown)) {
                    verifyStuff();
                    verifyOtherStuff();
                    alsoVerifyThis();
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                System.err.print("Reflection exception: " + e.getMessage());  // ugh...
            }
        }
    };

    @Before
    public void beforeEach() throws Exception {
        doAwesomeSetup();
    }
}

Copy link
Member

@kcooney kcooney left a comment

Choose a reason for hiding this comment

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

While I can't think of any problems, I think any user of JUnit 4.13 that uses Java 8 should use expectThrows() or assertThrows() instead of ExpectExceptoon

/**
* Check if any Exception is expected.
*/
public boolean isAnyExceptionExpected() {
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this final

@@ -245,6 +245,13 @@ public ExpectedException expectCause(Matcher<?> expectedCause) {
return this;
}

/**
* Check if any Exception is expected.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Add @since 4.13

@kcooney kcooney merged commit d07ed0b into junit-team:master May 6, 2017
sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
@stefanbirkner stefanbirkner changed the title Made ExpectedExceptio.isAnyExceptionExpected public. Made ExpectedException.isAnyExceptionExpected public. Oct 1, 2018
@stefanbirkner stefanbirkner changed the title Made ExpectedException.isAnyExceptionExpected public. Made ExpectedException.isAnyExceptionExpected public Oct 1, 2018
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
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.

3 participants