com.zmicer.utils.junit
Class JUnitUtils

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by com.zmicer.utils.junit.JUnitUtils
All Implemented Interfaces:
junit.framework.Test

public class JUnitUtils
extends junit.framework.TestCase

This class contains the functionality related to the JUnit itself and should :) be used for the increasing your productivity with junit.
It contains also the tests for these method. This class is test case itself so it is not a problem to store the tests here to. note [zmicer]: if it is becoming large one - move the tests to the separated test class

$Author:: $
$Rev:: $
$Date:: $


Field Summary
static org.apache.log4j.Logger LOG
          Logger instance.
 
Constructor Summary
JUnitUtils()
           
 
Method Summary
static void checkOnWrongArgs(Object object, String methodName, boolean[] requiredInfo, Class[] argsClasses, Object... args)
          Test the given method on the wrong args.
static String getRunningMethodName()
           
static String getRunningTestMethodName()
          todo [zmicer]: the current implementation of this functionality is not correct one.
static int getTestMethodsNumber(Class claz)
          Get the number of the tests methods at the provided class instance.
static void runFailure(Object object, String methodName, Class exceptionClaz, Class[] argsClasses, Object... arguments)
          Run the test which should ended with the fail result.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Logger LOG
Logger instance.

Constructor Detail

JUnitUtils

public JUnitUtils()
Method Detail

getTestMethodsNumber

public static int getTestMethodsNumber(Class claz)
Get the number of the tests methods at the provided class instance. todo [zmicer]: consider the necessarity of introducing the JUnitUtils class

Parameters:
claz - the class we need analyze. Can not be null (otherwise IllegalArgumentException would appear).
Returns:
the number of the test methods. In the case of this class has not method - zero (0) would be returned

checkOnWrongArgs

public static void checkOnWrongArgs(Object object,
                                    String methodName,
                                    boolean[] requiredInfo,
                                    Class[] argsClasses,
                                    Object... args)
Test the given method on the wrong args. It tests all the cases and compositions. It could be used for the unit testing as allows us not to think on the IllegalArgumentException checks - this method check all this by itself, including all the possible combinations and cases.
Be noticed that in the case of smth. wrong - e.g. invalid method or class was specified - the unit tests method fail is invoked with the description of what went wrong
Example of working of this method: there is method with two first required arguments and two last not required. Then we would check the following combinations:
method(null, new smth, null, null) method(new smth, null, null, null)
Be noticed this method accesses only the public method of the class you are testing using JUnit.

Parameters:
object - the object on which we would check. Can not be null (otherwise IllegalArgumentException would appear).
methodName - the method name we would test Can not be null (otherwise IllegalArgumentException would appear).
requiredInfo - defines which of the parameters are required (for which the IllegalArgumentException should be thrown) Can not be null (otherwise IllegalArgumentException would appear). The number of the members of this array should be equal to the number of the methodParamsClasses arg var
argsClasses - the Array of classes to be used, Can not be null (otherwise IllegalArgumentException would appear).
args - input arguments to be used on default (without null).

runFailure

public static void runFailure(Object object,
                              String methodName,
                              Class exceptionClaz,
                              Class[] argsClasses,
                              Object... arguments)
Run the test which should ended with the fail result.

Parameters:
object - object on which to run. Can not be null (otherwise IllegalArgumentException would appear).
methodName - method to be runned. Can not be null (otherwise IllegalArgumentException would appear).
exceptionClaz - exception to be thrown and then caught, Can not be null (otherwise IllegalArgumentException would appear).
argsClasses - classes of the arguments (to find the appropriate method..) Can not be null (otherwise IllegalArgumentException would appear).
arguments - the var args defining the arguments to be passed to the running method

getRunningTestMethodName

public static String getRunningTestMethodName()
todo [zmicer]: the current implementation of this functionality is not correct one. Still I consider it is possible to implement this. Review how it is done for the junit - it works. If you need this functionality somewhere - just adjust this sources note [zmicer]: only for the test purposes (for JPatterns test engine allowing to setup the props/xml environment per the folders)

note [zmicer]: Does not use regular expressions. On default this method without parameter uses the second method from the trace of the methods, still if you you this not directly but via another utility method building pathes e.g. for the unit test configuration please then use method with parameters.

Returns:
the name of the running method.

getRunningMethodName

public static String getRunningMethodName()
Returns:
the name of the method which is runned by the given unit test. This method should be used only for unit testing (only JUnit or maven unit tests plugin) and should be called inly at the test method itself (in simple words on the calls stack it should second after invoke0 method of the tests runner /zero/ and test method itself /first/)


Copyright © 2007 jzmicer.team. All Rights Reserved.