|
| 1 | +package org.hamcrest; |
| 2 | + |
| 3 | +import org.junit.AssumptionViolatedException; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.opentest4j.TestAbortedException; |
| 6 | + |
| 7 | +import static org.hamcrest.MatcherAssume.assumeThat; |
| 8 | +import static org.hamcrest.Matchers.startsWith; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.fail; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests compatibility with JUnit 5 with JUnit 4 <i>and</i> JUnit 5 on the classpath. |
| 14 | + * The equivalent test with only JUnit 4 on the classpath is in another module. |
| 15 | + */ |
| 16 | +class JUnit5MatcherAssumeTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void |
| 20 | + assumptionFailsWithMessage() { |
| 21 | + try { |
| 22 | + assumeThat("Custom assumption", "a", startsWith("abc")); |
| 23 | + fail("should have failed"); |
| 24 | + } |
| 25 | + catch (TestAbortedException e) { |
| 26 | + assertEquals("Assumption failed: Custom assumption", e.getMessage()); |
| 27 | + } |
| 28 | + catch (AssumptionViolatedException e) { |
| 29 | + // If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually |
| 30 | + // a false ignored test. |
| 31 | + throw new AssertionError("Illegal JUnit 4 assumption", e); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test void |
| 36 | + assumptionFailsWithDefaultMessage() { |
| 37 | + try { |
| 38 | + assumeThat("a", startsWith("abc")); |
| 39 | + fail("should have failed"); |
| 40 | + } |
| 41 | + catch (TestAbortedException e) { |
| 42 | + assertEquals("Assumption failed", e.getMessage()); |
| 43 | + } |
| 44 | + catch (AssumptionViolatedException e) { |
| 45 | + // If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually |
| 46 | + // a false ignored test. |
| 47 | + throw new AssertionError("Illegal JUnit 4 assumption", e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test void |
| 52 | + assumptionSucceeds() { |
| 53 | + try { |
| 54 | + assumeThat("xyz", startsWith("xy")); |
| 55 | + } |
| 56 | + catch (AssumptionViolatedException e) { |
| 57 | + // If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually |
| 58 | + // a false ignored test. |
| 59 | + throw new AssertionError("Illegal JUnit 4 assumption", e); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments