1818 */
1919public class BlockJUnit4ClassRunnerWithParameters extends
2020 BlockJUnit4ClassRunner {
21+ private enum InjectionType {
22+ CONSTRUCTOR , FIELD
23+ }
24+
2125 private final Object [] parameters ;
2226
2327 private final String name ;
@@ -32,10 +36,15 @@ public BlockJUnit4ClassRunnerWithParameters(TestWithParameters test)
3236
3337 @ Override
3438 public Object createTest () throws Exception {
35- if (fieldsAreAnnotated ()) {
36- return createTestUsingFieldInjection ();
37- } else {
38- return createTestUsingConstructorInjection ();
39+ InjectionType injectionType = getInjectionType ();
40+ switch (injectionType ) {
41+ case CONSTRUCTOR :
42+ return createTestUsingConstructorInjection ();
43+ case FIELD :
44+ return createTestUsingFieldInjection ();
45+ default :
46+ throw new IllegalStateException ("The injection type "
47+ + injectionType + " is not supported." );
3948 }
4049 }
4150
@@ -86,15 +95,15 @@ protected String testName(FrameworkMethod method) {
8695 @ Override
8796 protected void validateConstructor (List <Throwable > errors ) {
8897 validateOnlyOneConstructor (errors );
89- if (fieldsAreAnnotated () ) {
98+ if (getInjectionType () != InjectionType . CONSTRUCTOR ) {
9099 validateZeroArgConstructor (errors );
91100 }
92101 }
93102
94103 @ Override
95104 protected void validateFields (List <Throwable > errors ) {
96105 super .validateFields (errors );
97- if (fieldsAreAnnotated () ) {
106+ if (getInjectionType () == InjectionType . FIELD ) {
98107 List <FrameworkField > annotatedFieldsByParameter = getAnnotatedFieldsByParameter ();
99108 int [] usedIndices = new int [annotatedFieldsByParameter .size ()];
100109 for (FrameworkField each : annotatedFieldsByParameter ) {
@@ -137,6 +146,14 @@ private List<FrameworkField> getAnnotatedFieldsByParameter() {
137146 return getTestClass ().getAnnotatedFields (Parameter .class );
138147 }
139148
149+ private InjectionType getInjectionType () {
150+ if (fieldsAreAnnotated ()) {
151+ return InjectionType .FIELD ;
152+ } else {
153+ return InjectionType .CONSTRUCTOR ;
154+ }
155+ }
156+
140157 private boolean fieldsAreAnnotated () {
141158 return !getAnnotatedFieldsByParameter ().isEmpty ();
142159 }
0 commit comments