@@ -115,51 +115,54 @@ static void run(Class<? extends CracTest> testClass, String[] args) throws Excep
115115 argsOffset = 2 ;
116116 }
117117
118+ Constructor <? extends CracTest > ctor ;
118119 try {
119- Constructor <? extends CracTest > ctor = testClass .getConstructor ();
120- CracTest testInstance = ctor .newInstance ();
121- Field [] argFields = getArgFields (testClass );
122- for (int index = 0 ; index < argFields .length ; index ++) {
123- Field f = argFields [index ];
124- assertFalse (Modifier .isFinal (f .getModifiers ()), "@CracTestArg fields must not be final!" );
125- Class <?> t = f .getType ();
126- if (index + argsOffset >= args .length ) {
127- if (f .getAnnotation (CracTestArg .class ).optional ()) {
128- break ;
129- } else {
130- fail ("Not enough args for field " + f .getName () + "(" + index + "): have " + (args .length - argsOffset ));
131- }
132- }
133- String arg = args [index + argsOffset ];
134- Object value = arg ;
135- if (t == boolean .class || t == Boolean .class ) {
136- assertTrue ("true" .equals (arg ) || "false" .equals (arg ), "Argument " + index + "Boolean arg should be either 'true' or 'false', was: " + arg );
137- value = Boolean .parseBoolean (arg );
138- } else if (t == int .class || t == Integer .class ) {
139- try {
140- value = Integer .parseInt (arg );
141- } catch (NumberFormatException e ) {
142- fail ("Cannot parse argument '" + arg + "' as integer for @CracTestArg(" + index + ") " + f .getName ());
143- }
144- } else if (t == long .class || t == Long .class ) {
145- try {
146- value = Long .parseLong (arg );
147- } catch (NumberFormatException e ) {
148- fail ("Cannot parse argument '" + arg + "' as long for @CracTestArg(" + index + ") " + f .getName ());
149- }
150- } else if (t .isEnum ()) {
151- value = Enum .valueOf ((Class <Enum >) t , arg );
120+ ctor = testClass .getConstructor ();
121+ } catch (NoSuchMethodException e ) {
122+ fail ("Test class " + testClass .getName () + " is expected to have a public no-arg constructor" );
123+ return ; // Unreachable but makes the compiler sure ctor is always initialized below
124+ }
125+
126+ CracTest testInstance = ctor .newInstance ();
127+ Field [] argFields = getArgFields (testClass );
128+ for (int index = 0 ; index < argFields .length ; index ++) {
129+ Field f = argFields [index ];
130+ assertFalse (Modifier .isFinal (f .getModifiers ()), "@CracTestArg fields must not be final!" );
131+ Class <?> t = f .getType ();
132+ if (index + argsOffset >= args .length ) {
133+ if (f .getAnnotation (CracTestArg .class ).optional ()) {
134+ break ;
135+ } else {
136+ fail ("Not enough args for field " + f .getName () + "(" + index + "): have " + (args .length - argsOffset ));
152137 }
153- f .setAccessible (true );
154- f .set (testInstance , value );
155138 }
156- if (argsOffset == 0 ) {
157- testInstance .test ();
158- } else {
159- testInstance .exec ();
139+ String arg = args [index + argsOffset ];
140+ Object value = arg ;
141+ if (t == boolean .class || t == Boolean .class ) {
142+ assertTrue ("true" .equals (arg ) || "false" .equals (arg ), "Argument " + index + "Boolean arg should be either 'true' or 'false', was: " + arg );
143+ value = Boolean .parseBoolean (arg );
144+ } else if (t == int .class || t == Integer .class ) {
145+ try {
146+ value = Integer .parseInt (arg );
147+ } catch (NumberFormatException e ) {
148+ fail ("Cannot parse argument '" + arg + "' as integer for @CracTestArg(" + index + ") " + f .getName ());
149+ }
150+ } else if (t == long .class || t == Long .class ) {
151+ try {
152+ value = Long .parseLong (arg );
153+ } catch (NumberFormatException e ) {
154+ fail ("Cannot parse argument '" + arg + "' as long for @CracTestArg(" + index + ") " + f .getName ());
155+ }
156+ } else if (t .isEnum ()) {
157+ value = Enum .valueOf ((Class <Enum >) t , arg );
160158 }
161- } catch (NoSuchMethodException e ) {
162- fail ("Test class " + testClass .getName () + " is expected to have a public no-arg constructor" );
159+ f .setAccessible (true );
160+ f .set (testInstance , value );
161+ }
162+ if (argsOffset == 0 ) {
163+ testInstance .test ();
164+ } else {
165+ testInstance .exec ();
163166 }
164167 }
165168
0 commit comments