diff options
author | Jonathan Scott <scottjonathan@google.com> | 2021-08-18 17:14:27 +0100 |
---|---|---|
committer | Jonathan Scott <scottjonathan@google.com> | 2021-08-18 17:14:27 +0100 |
commit | f48709d2536ffab84303f7d233fefa16401b90f7 (patch) | |
tree | 9fd3e2913c92b64ab7f0403066556ad73202a5fc /src/test/java/com/google | |
parent | b51cebd6ba9ca7f524418ed3b2d9b2540308b8d7 (diff) | |
download | TestParameterInjector-f48709d2536ffab84303f7d233fefa16401b90f7.tar.gz |
Add support for custom test annotations and test sorting.
These changes will be included in a coming public release.
Test: atest TestParameterInjectorTest
Bug: 188893663
Change-Id: Ifd753a963dc8ed1c9a1ed24c879a91a5ec803ea7
Diffstat (limited to 'src/test/java/com/google')
-rw-r--r-- | src/test/java/com/google/testing/junit/testparameterinjector/PluggableTestRunnerTest.java | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/src/test/java/com/google/testing/junit/testparameterinjector/PluggableTestRunnerTest.java b/src/test/java/com/google/testing/junit/testparameterinjector/PluggableTestRunnerTest.java index 686b152..13561ff 100644 --- a/src/test/java/com/google/testing/junit/testparameterinjector/PluggableTestRunnerTest.java +++ b/src/test/java/com/google/testing/junit/testparameterinjector/PluggableTestRunnerTest.java @@ -15,9 +15,16 @@ package com.google.testing.junit.testparameterinjector; import static com.google.common.truth.Truth.assertThat; +import static java.util.Comparator.comparing; import com.google.common.collect.ImmutableList; + +import java.lang.annotation.Annotation; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; import org.junit.Rule; import org.junit.Test; import org.junit.rules.MethodRule; @@ -30,8 +37,11 @@ import org.junit.runners.model.Statement; @RunWith(JUnit4.class) public class PluggableTestRunnerTest { + @Retention(RetentionPolicy.RUNTIME) + private static @interface CustomTest {} private static int ruleInvocationCount = 0; + private static int testMethodInvocationCount = 0; public static class TestAndMethodRule implements MethodRule, TestRule { @@ -49,7 +59,7 @@ public class PluggableTestRunnerTest { } @RunWith(PluggableTestRunner.class) - public static class PluggableTestRunnerTestClass { + public static class TestAndMethodRuleTestClass { @Rule public TestAndMethodRule rule = new TestAndMethodRule(); @@ -62,7 +72,7 @@ public class PluggableTestRunnerTest { @Test public void ruleThatIsBothTestRuleAndMethodRuleIsInvokedOnceOnly() throws Exception { PluggableTestRunner.run( - new PluggableTestRunner(PluggableTestRunnerTestClass.class) { + new PluggableTestRunner(TestAndMethodRuleTestClass.class) { @Override protected List<TestMethodProcessor> createTestMethodProcessorList() { return ImmutableList.of(); @@ -71,4 +81,76 @@ public class PluggableTestRunnerTest { assertThat(ruleInvocationCount).isEqualTo(1); } + + @RunWith(PluggableTestRunner.class) + public static class CustomTestAnnotationTestClass { + @SuppressWarnings("JUnit4TestNotRun") + @CustomTest + public void customTestAnnotatedTest() { + testMethodInvocationCount++; + } + + @Test + public void testAnnotatedTest() { + testMethodInvocationCount++; + } + } + + @Test + public void testMarkedWithCustomClassIsInvoked() throws Exception { + testMethodInvocationCount = 0; + PluggableTestRunner.run( + new PluggableTestRunner(CustomTestAnnotationTestClass.class) { + @Override + protected List<TestMethodProcessor> createTestMethodProcessorList() { + return ImmutableList.of(); + } + + @Override + protected ImmutableList<Class<? extends Annotation>> getSupportedTestAnnotations() { + return ImmutableList.of(Test.class, CustomTest.class); + } + }); + + assertThat(testMethodInvocationCount).isEqualTo(2); + } + + private static final List<String> testOrder = new ArrayList<>(); + + @RunWith(PluggableTestRunner.class) + public static class SortedPluggableTestRunnerTestClass { + @Test + public void a() { + testOrder.add("a"); + } + + @Test + public void b() { + testOrder.add("b"); + } + + @Test + public void c() { + testOrder.add("c"); + } + } + + @Test + public void testsAreSortedCorrectly() throws Exception { + testOrder.clear(); + PluggableTestRunner.run( + new PluggableTestRunner(SortedPluggableTestRunnerTestClass.class) { + @Override + protected List<TestMethodProcessor> createTestMethodProcessorList() { + return ImmutableList.of(); + } + + @Override + protected Stream<FrameworkMethod> sortTestMethods(Stream<FrameworkMethod> methods) { + return methods.sorted(comparing(FrameworkMethod::getName).reversed()); + } + }); + assertThat(testOrder).containsExactly("c", "b", "a"); + } + } |