Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.callbackparams.junit4;
 
 import java.util.List;
The very simpliest use-case for callback-params is when the parameters are self-contained, i.e. they have at least one regular JUnit4 test-method (takes zero arguments) which can perform its test all on its own without input from any containing callback-params test-method. For this case it could be over-kill to have the parameters go the detour through some customized CallbackParams test-class. However, by using this test-runner class for the enum that provides these parameters it is possible to run the self-contained test-methods without writing any additional code.

It is not absolutly mandatory for the test-class to be an enum. - The important thing is that it has a static method "values()" that returns the test-instances. I.e. for an enum-class the test-instances would then turn out to be the enum-constants.

The implementation is pretty much the simpliest possible by taking advantage of features available in the runner-class BlockJUnit4ClassRunner. The downside of this is that it demands JUnit version 4.5 or later ...

Author(s):
Henrik Kaipe
 
 public class EnumRunner extends ParentRunner<BlockJUnit4ClassRunner> {
 
     private final List<BlockJUnit4ClassRunnerchildren;
     
     public EnumRunner(Class<? extends EnumtestClass)
     throws InitializationError {
         super(testClass);
         Object[] enumValues = CallbackRecordsFactory.getInstance()
                 .retrieveCallbackValuesArray(testClass);
         this. = new ArrayList<BlockJUnit4ClassRunner>(enumValues.length);
         for (Object testInstance : enumValues) {
             this..add(
                     new SingleEnumValueRunner(testClasstestInstance));
         }
     }
 
     @Override
     protected List<BlockJUnit4ClassRunnergetChildren() {
         return this.;
     }
 
     @Override
     protected Description describeChild(BlockJUnit4ClassRunner child) {
         return child.getDescription();
     }
 
     @Override
     protected void runChild(
             BlockJUnit4ClassRunner childRunNotifier notifier) {
         child.run(notifier);
     }
 
     private static class SingleEnumValueRunner
     extends BlockJUnit4ClassRunner {
         
         private final Object testValue;
 
         public SingleEnumValueRunner(Class<?> testClassObject testValue)
         throws InitializationError {
             super(testClass);
             this. = testValue;
         }
 
         @Override
         protected Object createTest() {
             return ;
         }
 
         @Override
         protected String testName(FrameworkMethod method) {
            return super.testName(method) + '[' +  + ']';
        }
        @Override
        
Overridden to make sure that no constructor validations are performed.
        protected void collectInitializationErrors(List<Throwableerrors) {
            validateInstanceMethods(errors);
        }
    }
New to GrepCode? Check out our FAQ X