Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nipa
committed
Jan 6, 2017
1 parent
b91baa5
commit 0cf3183
Showing
7 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/codefx/demo/junit5/DisabledCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.codefx.demo.junit5; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
import org.junit.jupiter.api.extension.ContainerExecutionCondition; | ||
import org.junit.jupiter.api.extension.ContainerExtensionContext; | ||
import org.junit.jupiter.api.extension.TestExecutionCondition; | ||
import org.junit.jupiter.api.extension.TestExtensionContext; | ||
import org.junit.platform.commons.util.AnnotationUtils; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.Optional; | ||
|
||
/** | ||
* An exemplary reimplementation of the extension supporting the {@link Disabled @Disabled} annotation. | ||
*/ | ||
public class DisabledCondition implements ContainerExecutionCondition, TestExecutionCondition { | ||
|
||
private static final ConditionEvaluationResult ENABLED = | ||
ConditionEvaluationResult.enabled("@Disabled is not present"); | ||
|
||
@Override | ||
public ConditionEvaluationResult evaluate(ContainerExtensionContext context) { | ||
return evaluateIfAnnotated(context.getElement()); | ||
} | ||
|
||
@Override | ||
public ConditionEvaluationResult evaluate(TestExtensionContext context) { | ||
return evaluateIfAnnotated(context.getElement()); | ||
} | ||
|
||
private ConditionEvaluationResult evaluateIfAnnotated(Optional<AnnotatedElement> element) { | ||
return element.filter(el -> AnnotationUtils.isAnnotated(el, Disabled.class)) | ||
.map(el -> ConditionEvaluationResult.disabled(el + " is @Disabled")) | ||
.orElse(ENABLED); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,6 @@ | |
@ExtendWith(OsCondition.class) | ||
public @interface DisabledOnOs { | ||
|
||
OS[] value() default {}; | ||
OS[] value(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,6 @@ | |
@Test | ||
public @interface TestExceptOnOs { | ||
|
||
OS[] value() default {}; | ||
OS[] value(); | ||
|
||
} |
24 changes: 10 additions & 14 deletions
24
src/test/java/org/codefx/demo/junit5/extensions/DisabledIfFailsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
package org.codefx.demo.junit5.extensions; | ||
|
||
import org.codefx.demo.junit5.CollectExceptions; | ||
import org.codefx.demo.junit5.DisabledIfTestFailedWith; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
@CollectExceptions | ||
@DisabledIfTestFailedWith(RuntimeException.class) | ||
class DisabledIfFailsTest { | ||
|
||
private static boolean failedFirst = false; | ||
private static boolean ONE_TEST_FAILED = false; | ||
|
||
@Test | ||
void a_throwException() { | ||
System.out.println("I failed!"); | ||
failedFirst = true; | ||
throw new RuntimeException(); | ||
void assertNoTestFailed_thenFail_1() { | ||
assertThenFail(); | ||
} | ||
|
||
@Test | ||
void b_disableIfOtherFailedFirst() { | ||
|
||
/* | ||
* TODO it looks like this does not work either. | ||
*/ | ||
void assertNoTestFailed_thenFail_2() { | ||
assertThenFail(); | ||
} | ||
|
||
System.out.println("Nobody failed yet! (Right?)"); | ||
assertFalse(failedFirst); | ||
private void assertThenFail() { | ||
assertFalse(ONE_TEST_FAILED, "No test should run after another failed!"); | ||
ONE_TEST_FAILED = true; | ||
throw new RuntimeException("I'm failing on purpose."); | ||
} | ||
|
||
} |