org.easetech.easytest.runner
Class SpringTestRunner

java.lang.Object
  extended by org.junit.runner.Runner
      extended by org.junit.runners.ParentRunner<org.junit.runners.model.FrameworkMethod>
          extended by org.junit.runners.BlockJUnit4ClassRunner
              extended by org.springframework.test.context.junit4.SpringJUnit4ClassRunner
                  extended by org.easetech.easytest.runner.SpringTestRunner
All Implemented Interfaces:
org.junit.runner.Describable, org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Sortable

public class SpringTestRunner
extends org.springframework.test.context.junit4.SpringJUnit4ClassRunner

A SpringJUnit4ClassRunner Runner extension that adds support of input parameters as part of the Test annotation. This SpringJUnit4ClassRunner extension is modified for providing convenient Data Driven Testing support to its users. This Runner is capable of generating new instances of FrameworkMethod based on the test data for a given method. For eg. If there is a method "testMethod(String testData)" that has three sets of test data : [{"testData1"},{"testData2"},{"testData3"}], then this runner will generate three FrameworkMethod instances with the method names :
testMethod{testData1}
testMethod{testData2}
and
testMethod{testData3}


A user can specify the test data at the class level, using the DataLoader annotation and override it at the method level. The Runner will take care of executing the test method with the right test data.
This is extremely beneficial in cases, where the user just wants to load the data once and then reuse it for all the test methods. If the user wants, then he can always override the test data at the method level by specifying the DataLoader annotation at the method level.

In addition, this runner also introduces a new way for the user to specify the test data using DataLoader annotation.

There is also a Param annotation to handle boiler plate tasks on behalf of the user as well as supports additional functionality that eases the life of the user. For eg. it supports Java PropertyEditors to automatically convert a String to the specified Object. It also supports passing a Map to the test method that contains all the available test data key / value pairs for easy consumption by the user. It also supports user defined custom Objects as parameters.

Author:
Anuj Kumar

Field Summary
protected static org.slf4j.Logger LOG
          An instance of logger associated with the test framework.
 
Constructor Summary
SpringTestRunner(Class<?> clazz)
          Construct a new NewSpringTestRunner
 
Method Summary
protected  List<org.junit.runners.model.FrameworkMethod> computeMethodsForTest()
          Overridden the compute test method to make it save the method list as class instance, so that the method does not run multiple times.
protected  List<org.junit.runners.model.FrameworkMethod> computeTestMethods()
          Compute any test methods
protected  Object createTest()
          Returns a new fixture for running a test.
protected  ClassLoader determineClassLoader(Class<?> fieldType, Class<?> testClass)
          Determine the right class loader to use to load the class
 void filter(org.junit.runner.manipulation.Filter filter)
          Override the filter method from ParentRunner so that individual tests can be run using EasyTest
protected  Object getTestInstance()
          Get the instance of the class under test
protected  void instrumentClass(Class<?> testClass, Object testInstance)
          Instrument the class's field that are marked with Intercept annotation
protected  void loadResourceProperties(Object testInstance)
           
protected  void loadTestConfigurations(Object testInstance)
           
protected  org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod method)
           
protected  org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod method, Object testInstance)
          Returns a Statement that invokes method on test
 void registerConverter(org.easetech.easytest.annotation.Converters converter)
          Method responsible for registering the converters with the EasyTest framework
protected  void setSchedulingStrategy()
          Set whether the tests should be run in parallel or serial.
protected  String testName(org.junit.runners.model.FrameworkMethod method)
          Override the name of the test.
protected  void validateConstructor(List<Throwable> errors)
          Validate that there could ever be only one constructor.
protected  void validateInstanceMethods(List<Throwable> errors)
          Deprecated. unused API, will go away in future version
protected  void validateTestMethods(List<Throwable> errors)
          Validate the test methods.
protected  org.junit.runners.model.Statement withAfterClasses(org.junit.runners.model.Statement statement)
          Returns a Statement: run all non-overridden @AfterClass methods on this class and superclasses before executing statement; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into a MultipleFailureException .
protected  org.junit.runners.model.Statement withTestResult(org.easetech.easytest.runner.EasyFrameworkMethod method, org.junit.runners.model.Statement statement)
           
 
Methods inherited from class org.springframework.test.context.junit4.SpringJUnit4ClassRunner
createTestContextManager, getDefaultContextLoaderClassName, getDescription, getExpectedException, getJUnitTimeout, getSpringTimeout, getTestContextManager, isTestMethodIgnored, possiblyExpectingExceptions, run, runChild, withAfters, withBeforeClasses, withBefores, withPotentialRepeat, withPotentialTimeout
 
Methods inherited from class org.junit.runners.BlockJUnit4ClassRunner
collectInitializationErrors, describeChild, getChildren, getTestRules, rules, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateZeroArgConstructor
 
Methods inherited from class org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, getName, getRunnerAnnotations, getTestClass, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods
 
Methods inherited from class org.junit.runner.Runner
testCount
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static final org.slf4j.Logger LOG
An instance of logger associated with the test framework.

Constructor Detail

SpringTestRunner

public SpringTestRunner(Class<?> clazz)
                 throws org.junit.runners.model.InitializationError
Construct a new NewSpringTestRunner

Parameters:
clazz -
Throws:
org.junit.runners.model.InitializationError
Method Detail

setSchedulingStrategy

protected void setSchedulingStrategy()
Set whether the tests should be run in parallel or serial.


computeMethodsForTest

protected List<org.junit.runners.model.FrameworkMethod> computeMethodsForTest()
Overridden the compute test method to make it save the method list as class instance, so that the method does not run multiple times. Also, this method now is responsible for creating multiple FrameworkMethod instances for a given method with multiple test data. So, if a given test method needs to run three times with three set of input test data, then this method will actually create three instances of FrameworkMethod. In order to allow the user to override the default name, FrameworkMethod is extended with EasyFrameworkMethod and EasyFrameworkMethod.setName(String) method introduced.

Returns:
list of FrameworkMethod

getTestInstance

protected Object getTestInstance()
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException
Get the instance of the class under test

Returns:
the instance of class under test
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException

loadTestConfigurations

protected void loadTestConfigurations(Object testInstance)
See Also:
TestConfigUtil.loadTestConfigurations(Class, Object)

loadResourceProperties

protected void loadResourceProperties(Object testInstance)
See Also:
TestConfigUtil.loadResourceProperties(java.lang.Class, java.lang.Object)

computeTestMethods

protected List<org.junit.runners.model.FrameworkMethod> computeTestMethods()
Compute any test methods

Overrides:
computeTestMethods in class org.junit.runners.BlockJUnit4ClassRunner
Returns:
a list of FrameworkMethods

filter

public void filter(org.junit.runner.manipulation.Filter filter)
            throws org.junit.runner.manipulation.NoTestsRemainException
Override the filter method from ParentRunner so that individual tests can be run using EasyTest

Specified by:
filter in interface org.junit.runner.manipulation.Filterable
Overrides:
filter in class org.junit.runners.ParentRunner<org.junit.runners.model.FrameworkMethod>
Parameters:
filter -
Throws:
org.junit.runner.manipulation.NoTestsRemainException

instrumentClass

protected void instrumentClass(Class<?> testClass,
                               Object testInstance)
                        throws IllegalArgumentException,
                               IllegalAccessException,
                               org.springframework.aop.framework.AopConfigException,
                               InstantiationException
Instrument the class's field that are marked with Intercept annotation

Parameters:
testClass - the class under test
Throws:
IllegalArgumentException - if an exception occurred
IllegalAccessException - if an exception occurred
org.springframework.aop.framework.AopConfigException - if an exception occurred
InstantiationException - if an exception occurred

registerConverter

public void registerConverter(org.easetech.easytest.annotation.Converters converter)
Method responsible for registering the converters with the EasyTest framework

Parameters:
converter - the annotation Converters

testName

protected String testName(org.junit.runners.model.FrameworkMethod method)
Override the name of the test. In case of EasyTest, it will be the name of the test method concatenated with the input test data that the method will run with.

Overrides:
testName in class org.junit.runners.BlockJUnit4ClassRunner
Parameters:
method - the FrameworkMethod
Returns:
an overridden test method Name

validateConstructor

protected void validateConstructor(List<Throwable> errors)
Validate that there could ever be only one constructor.

Overrides:
validateConstructor in class org.junit.runners.BlockJUnit4ClassRunner
Parameters:
errors - list of any errors while validating the Constructor

validateInstanceMethods

@Deprecated
protected void validateInstanceMethods(List<Throwable> errors)
Deprecated. unused API, will go away in future version

Adds to errors for each method annotated with @Test, @Before, or @After that is not a public, void instance method with no arguments.

Overrides:
validateInstanceMethods in class org.junit.runners.BlockJUnit4ClassRunner

validateTestMethods

protected void validateTestMethods(List<Throwable> errors)
Validate the test methods.

Overrides:
validateTestMethods in class org.junit.runners.BlockJUnit4ClassRunner
Parameters:
errors - list of any errors while validating test method

methodBlock

protected org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod method)
Overrides:
methodBlock in class org.springframework.test.context.junit4.SpringJUnit4ClassRunner

withTestResult

protected org.junit.runners.model.Statement withTestResult(org.easetech.easytest.runner.EasyFrameworkMethod method,
                                                           org.junit.runners.model.Statement statement)

createTest

protected Object createTest()
                     throws Exception
Returns a new fixture for running a test. Default implementation executes the test class's no-argument constructor (validation should have ensured one exists).

Overrides:
createTest in class org.springframework.test.context.junit4.SpringJUnit4ClassRunner
Throws:
Exception

methodInvoker

protected org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod method,
                                                          Object testInstance)
Returns a Statement that invokes method on test

Overrides:
methodInvoker in class org.junit.runners.BlockJUnit4ClassRunner

withAfterClasses

protected org.junit.runners.model.Statement withAfterClasses(org.junit.runners.model.Statement statement)
Returns a Statement: run all non-overridden @AfterClass methods on this class and superclasses before executing statement; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into a MultipleFailureException . This method is also responsible for writing the data to the output file in case the user is returning test data from the test method. This method will make sure that the data is written to the output file once after the Runner has completed and not for every instance of the test method.

Overrides:
withAfterClasses in class org.springframework.test.context.junit4.SpringJUnit4ClassRunner

determineClassLoader

protected ClassLoader determineClassLoader(Class<?> fieldType,
                                           Class<?> testClass)
Determine the right class loader to use to load the class

Parameters:
fieldType -
testClass -
Returns:
the classLoader or null if none found


Copyright © 2013. All Rights Reserved.