Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using value method instead of values in TestFactory.

Added unit test for CustomizableJUnitClassRunner
  • Loading branch information...
commit 484292e7575db4049cb918b38b9744b15dc8683c 1 parent 94835b7
Vasiliy Gagin authored
9 src/main/java/org/junit/experimental/runners/customizable/CustomizableJUnit4ClassRunner.java
View
@@ -63,7 +63,7 @@ public CustomizableJUnit4ClassRunner(Class<?> klass)
if (annotation != null) {
try {
for (Class<? extends TestFactory> testFactoryClass : annotation
- .values()) {
+ .value()) {
result.add(testFactoryClass.newInstance());
}
} catch (Throwable e) {
@@ -176,12 +176,13 @@ protected void validateMethods(List<Throwable> errors) {
}
@Override
- protected Description describeChild(FrameworkTest method) {
- return method.createDescription();
+ protected Description describeChild(FrameworkTest frameworkTest) {
+ return frameworkTest.createDescription();
}
@Override
- protected void runChild(final FrameworkTest frameworkTest, RunNotifier notifier) {
+ protected void runChild(final FrameworkTest frameworkTest,
+ RunNotifier notifier) {
Description description= describeChild(frameworkTest);
if (frameworkTest.isIgnored()) {
notifier.fireTestIgnored(description);
2  src/main/java/org/junit/experimental/runners/customizable/TestFactories.java
View
@@ -16,5 +16,5 @@
@Inherited
public @interface TestFactories {
- Class<? extends TestFactory>[] values() default { ReflectionTestFactory.class };
+ Class<? extends TestFactory>[] value() default { ReflectionTestFactory.class };
}
160 src/test/java/org/junit/experimental/runners/customizable/CustomizableJUnit4ClassRunnerTest.java
View
@@ -0,0 +1,160 @@
+package org.junit.experimental.runners.customizable;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+import org.junit.runners.model.TestClass;
+
+public class CustomizableJUnit4ClassRunnerTest {
+
+ @Test
+ public void discoverTestFactories_shouldDefaultToReflectionTestFactory()
+ throws InitializationError {
+ new CustomizableJUnit4ClassRunner(DefaultTestClass.class) {
+
+ @Override
+ protected List<TestFactory> discoverTestFactories(Class<?> klass)
+ throws InitializationError {
+ List<TestFactory> testFactories= super
+ .discoverTestFactories(klass);
+ assertNotNull(testFactories);
+ assertEquals(1, testFactories.size());
+ assertEquals(ReflectionTestFactory.class, testFactories.get(0)
+ .getClass());
+ return testFactories;
+ }
+ };
+ }
+
+ @Test
+ public void discoverTestFactories_shouldUseAnnotationIfProvided()
+ throws InitializationError {
+ new CustomizableJUnit4ClassRunner(AnnotatedTestClass.class) {
+
+ @Override
+ protected List<TestFactory> discoverTestFactories(Class<?> klass)
+ throws InitializationError {
+ List<TestFactory> testFactories= super
+ .discoverTestFactories(klass);
+ assertNotNull(testFactories);
+ assertEquals(1, testFactories.size());
+ assertEquals(LocalTestFactory.class, testFactories.get(0)
+ .getClass());
+ return testFactories;
+ }
+ };
+ }
+
+ @Test
+ public void describeChild_shouldDelegateToFrameworkTest()
+ throws InitializationError {
+ final Description description= Description.createSuiteDescription("MY");
+ LocalFrameworkTest localTest= new LocalFrameworkTest() {
+
+ @Override
+ public Description createDescription() {
+ return description;
+ }
+ };
+ CustomizableJUnit4ClassRunner runner= new CustomizableJUnit4ClassRunner(
+ AnnotatedTestClass.class,
+ Arrays.<TestFactory> asList(new LocalTestFactory(localTest)));
+ Description actual= runner.describeChild(localTest);
+ assertSame(description, actual);
+ }
+
+ @Test
+ public void runChild_shouldConsultFrameworkTestForIgnore()
+ throws InitializationError {
+ final Description localDescription= Description
+ .createSuiteDescription("MY");
+ LocalFrameworkTest localTest= new LocalFrameworkTest() {
+ @Override
+ public Description createDescription() {
+ return localDescription;
+ }
+
+ @Override
+ public boolean isIgnored() {
+ return true;
+ }
+
+ };
+ CustomizableJUnit4ClassRunner runner= new CustomizableJUnit4ClassRunner(
+ AnnotatedTestClass.class,
+ Arrays.<TestFactory> asList(new LocalTestFactory(localTest)));
+ RunNotifier notifier= new RunNotifier() {
+
+ @Override
+ public void fireTestIgnored(Description description) {
+ assertSame(localDescription, description);
+ throw new SUCCESS();
+ }
+ };
+ try {
+ runner.runChild(localTest, notifier);
+ fail("Test was not ignored");
+ } catch (SUCCESS exc) {
+ }
+ }
+
+ public static class DefaultTestClass {
+
+ @Test
+ public void test() {
+ }
+ }
+
+ @TestFactories({ LocalTestFactory.class })
+ public static class AnnotatedTestClass {
+
+ @Test
+ public void test() {
+ }
+ }
+
+ public static class LocalFrameworkTest implements FrameworkTest {
+
+ public Description createDescription() {
+ return null;
+ }
+
+ public boolean isIgnored() {
+ return false;
+ }
+
+ public Statement createStatement(Object testInstance,
+ List<TestRule> testRules) {
+ return null;
+ }
+
+ }
+
+ public static class LocalTestFactory implements TestFactory {
+ private final LocalFrameworkTest test;
+
+ public LocalTestFactory() {
+ test= new LocalFrameworkTest();
+ }
+
+ public LocalTestFactory(LocalFrameworkTest test) {
+ this.test= test;
+ }
+
+ public List<FrameworkTest> computeTestMethods(TestClass testClass,
+ List<Throwable> errors) {
+ return Arrays.<FrameworkTest> asList(test);
+ }
+ }
+
+ private static class SUCCESS extends RuntimeException {
+ }
+}
2  src/test/java/org/junit/experimental/runners/customizable/example/FruitPickerTest.java
View
@@ -9,7 +9,7 @@
import org.junit.runner.RunWith;
@RunWith(CustomizableJUnit4ClassRunner.class)
-@TestFactories(values= { ReflectionTestFactory.class, EnumTestFactory.class })
+@TestFactories({ ReflectionTestFactory.class, EnumTestFactory.class })
public class FruitPickerTest {
private final FruitPicker unit= new FruitPicker();
Please sign in to comment.
Something went wrong with that request. Please try again.