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

Handle JUnit4 ExpectedException Rule as part of JUnit4 -> 5 recipe #9

Closed
sambsnyd opened this issue Nov 25, 2020 · 0 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@sambsnyd
Copy link
Member

sambsnyd commented Nov 25, 2020

org.junit.rules.ExpectedException appears in a few places in Design Partner 1's code base.

Per its documentation, ExpectedException is used like this:

                package org.openrewrite.java.testing.junit5;

                import org.junit.Rule;
                import org.junit.Test;
                import org.junit.rules.ExpectedException;
                
                public class SimpleExpectedExceptionTest {
                    @Rule
                    public ExpectedException thrown = ExpectedException.none();
                
                    @Test
                    public void throwsNothing() {
                        // no exception expected, none thrown: passes.
                    }
                
                    @Test
                    public void throwsExceptionWithSpecificType() {
                        thrown.expect(NullPointerException.class);
                        throw new NullPointerException();
                    }
                }

One way of translating this to JUnit 5 is:

                package org.openrewrite.java.testing.junit5;
                
                import org.junit.jupiter.api.Test;
                
                import static org.junit.jupiter.api.Assertions.assertThrows;
                
                public class SimpleExpectedExceptionTest {
                    @Test
                    public void throwsNothing() {
                        // no exception expected, none thrown: passes.
                    }
                
                    @Test
                    public void throwsExceptionWithSpecificType() {
                        assertThrows(NullPointerException.class, () -> {
                            throw new NullPointerException();
                        });
                    }
                }
@sambsnyd sambsnyd added the enhancement New feature or request label Nov 25, 2020
@sambsnyd sambsnyd self-assigned this Nov 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant