Sponsored Link •
|
Advertisement
|
Here is the entire test class that tests private method Runner.parseArgsIntoLists
, which
is described in the article Testing Private Methods in JUnit and SuiteRunner.
/* * Copyright (C) 2001-2003 Artima Software, Inc. All rights reserved. * Licensed under the Open Software License version 1.0. * * A copy of the Open Software License version 1.0 is available at: * http://www.artima.com/suiterunner/osl10.html * * This software consists of voluntary contributions made by many * individuals on behalf of Artima Software, Inc. For more * information on Artima Software, Inc., please see: * http://www.artima.com/ */ package org.suiterunner; import java.lang.Throwable; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.io.IOException; import java.util.Date; import java.util.List; import java.util.ArrayList; public class RunnerSuite extends Suite { private static void invokeStaticMethod(Class targetClass, String methodName, Class[] argClasses, Object[] argObjects) throws InvocationTargetException { try { Method method = targetClass.getDeclaredMethod(methodName, argClasses); method.setAccessible(true); method.invoke(null, argObjects); } catch (NoSuchMethodException e) { // Should happen only rarely, because most times the // specified method should exist. If it does happen, just let // the test fail so the programmer can fix the problem. throw new TestFailedException(e); } catch (SecurityException e) { // Should happen only rarely, because the setAccessible(true) // should be allowed in when running unit tests. If it does // happen, just let the test fail so the programmer can fix // the problem. throw new TestFailedException(e); } catch (IllegalAccessException e) { // Should never happen, because setting accessible flag to // true. If setting accessible fails, should throw a security // exception at that point and never get to the invoke. But // just in case, wrap it in a TestFailedException and let a // human figure it out. throw new TestFailedException(e); } catch (IllegalArgumentException e) { // Should happen only rarerly, because usually the right // number and types of arguments will be passed. If it does // happen, just let the test fail so the programmer can fix // the problem. throw new TestFailedException(e); } } private static void invokeParseArgsIntoLists(String[] args, List runpathList, List reportersList, List suitesList) throws InvocationTargetException { // Purposely pass null values to the method, to make sure it throws // NullPointerException Class[] argClasses = {String[].class, List.class, List.class, List.class }; Object[] argObjects = {args, runpathList, reportersList, suitesList }; invokeStaticMethod(Runner.class, "parseArgsIntoLists", argClasses, argObjects); } public void testParseArgsIntoLists() throws InvocationTargetException { String[] args = new String[0]; List runpathList = new ArrayList(); List reportersList = new ArrayList(); List suitesList = new ArrayList(); try { invokeParseArgsIntoLists(null, runpathList, reportersList, suitesList); fail(); } catch (InvocationTargetException e) { // throw the InvocationTargetException unless the target // exception is NullPointerException, which is expected Throwable targetException = e.getTargetException(); if (!(targetException instanceof NullPointerException)) { throw e; } } try { invokeParseArgsIntoLists(args, null, reportersList, suitesList); fail(); } catch (InvocationTargetException e) { // throw the InvocationTargetException unless the target // exception is NullPointerException, which is expected Throwable targetException = e.getTargetException(); if (!(targetException instanceof NullPointerException)) { throw e; } } try { invokeParseArgsIntoLists(args, runpathList, null, suitesList); fail(); } catch (InvocationTargetException e) { // throw the InvocationTargetException unless the target // exception is NullPointerException, which is expected Throwable targetException = e.getTargetException(); if (!(targetException instanceof NullPointerException)) { throw e; } } try { invokeParseArgsIntoLists(args, runpathList, reportersList, null); fail(); } catch (InvocationTargetException e) { // throw the InvocationTargetException unless the target // exception is NullPointerException, which is expected Throwable targetException = e.getTargetException(); if (!(targetException instanceof NullPointerException)) { throw e; } } args = new String[7]; args[0] = "-p"; args[1] = "\"mydir\""; args[2] = "-g"; args[3] = "-f"; args[4] = "test.out"; args[5] = "-s"; args[6] = "MySuite"; runpathList.clear(); reportersList.clear(); suitesList.clear(); invokeParseArgsIntoLists(args, runpathList, reportersList, suitesList); verify(runpathList.size() == 2); verify(runpathList.get(0).equals(args[0])); verify(runpathList.get(1).equals(args[1])); verify(reportersList.size() == 3); verify(reportersList.get(0).equals(args[2])); verify(reportersList.get(1).equals(args[3])); verify(reportersList.get(2).equals(args[4])); verify(suitesList.size() == 2); verify(suitesList.get(0).equals(args[5])); verify(suitesList.get(1).equals(args[6])); args = new String[9]; args[0] = "-p"; args[1] = "\"mydir\""; args[2] = "-e"; args[3] = "-o"; args[4] = "-r"; args[5] = "MyCustomReporter"; args[6] = "-s"; args[7] = "MySuite"; args[8] = "MyOtherSuite"; runpathList.clear(); reportersList.clear(); suitesList.clear(); invokeParseArgsIntoLists(args, runpathList, reportersList, suitesList); verify(runpathList.size() == 2); verify(runpathList.get(0).equals(args[0])); verify(runpathList.get(1).equals(args[1])); verify(reportersList.size() == 4); verify(reportersList.get(0).equals(args[2])); verify(reportersList.get(1).equals(args[3])); verify(reportersList.get(2).equals(args[4])); verify(reportersList.get(3).equals(args[5])); verify(suitesList.size() == 3); verify(suitesList.get(0).equals(args[6])); verify(suitesList.get(1).equals(args[7])); verify(suitesList.get(2).equals(args[8])); args = new String[10]; args[0] = "-p"; args[1] = "\"serviceuitest-1.1beta4.jar myjini http://myhost:9998/myfile.jar\""; args[2] = "-g"; args[3] = "-s"; args[4] = "MySuite"; args[5] = "MySecondSuite"; args[6] = "MyThirdSuite"; args[7] = "MyFourthSuite"; args[8] = "MyFifthSuite"; args[9] = "MySixthSuite"; runpathList.clear(); reportersList.clear(); suitesList.clear(); invokeParseArgsIntoLists(args, runpathList, reportersList, suitesList); verify(runpathList.size() == 2); verify(runpathList.get(0).equals(args[0])); verify(runpathList.get(1).equals(args[1])); verify(reportersList.size() == 1); verify(reportersList.get(0).equals(args[2])); verify(suitesList.size() == 7); verify(suitesList.get(0).equals(args[3])); verify(suitesList.get(1).equals(args[4])); verify(suitesList.get(2).equals(args[5])); verify(suitesList.get(3).equals(args[6])); verify(suitesList.get(4).equals(args[7])); verify(suitesList.get(5).equals(args[8])); verify(suitesList.get(6).equals(args[9])); } }
Sponsored Links
|