title: TestNG @Dataprovider
body: <p>I have the following requirements for my Java project without TestNG but I added the <code>@Test</code> Annotation to run the class.</p>
  
  <pre><code>1. Find the classes which are all annotated with `@controller` in the class path
  2. Find the methods which are annotated with `@Requestmapping`
  3. Infer all the method properties for each classes
  4. Load Known details from Excel(Method name, HttpResponsecode, username, password)
  5. Send HttpPost or HttpGet or HttpPUT
  </code></pre>
  
  <p>Now I would like the convert the above functionality from 1 to 4 into <code>@DataProvider</code> and call the <code>@Test</code> Method. How can I do this?</p>
  
  <p>Sample code is :</p>
  
  <pre><code>package com.hexgen.reflection.integration;
  
  import java.io.BufferedWriter;
  import java.io.FileWriter;
  import java.lang.reflect.Method;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.http.client.HttpClient;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  import org.springframework.security.access.prepost.PreAuthorize;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  
  import com.hexgen.reflection.request.MethodInfoRO;
  import com.hexgen.reflection.request.MethodParamsInfoRO;
  import com.hexgen.reflection.support.HexgenControllerClassFinder;
  import com.hexgen.reflection.support.HexgenWebAPITestConstants;
  import com.hexgen.reflection.support.LoadMethodDetailsInfoFromExcel;
  
  /**
   *  
   * @author anthony
   *
   */
  public class HexgenWebAPITest {
  
      /**
       * This class finds the method which are not secured 
       * and makes sure that the method which are secured works properly
       */
  
      @SuppressWarnings({ "rawtypes", "unchecked" })
      @Test
      public void hexgenWebAPITest( ) {
  
          int statusCode=0;
          String[] requestMappingValues=null;
          String[] parametersDefinedForMethod=null;
          String strClassname="";
          String strClassNameToFix="";
          String requestingMethod="";
          String uri="";
  
          HttpClient client = new DefaultHttpClient();
          List&lt;MethodParamsInfoRO&gt; tempParamsList = null;
          List&lt;String&gt; notSecuredMethodsList = new ArrayList&lt;String&gt;();
          Map&lt;String,String&gt; requestingMethodMap = new LinkedHashMap&lt;String,String&gt;();
          Map&lt;String,String&gt; methodsMap = new LinkedHashMap&lt;String, String&gt;();
          Map&lt;String,List&gt; paramsDetailsMap = new LinkedHashMap&lt;String, List&gt;();
          Map&lt;String,String&gt; urlDetailsMap = new LinkedHashMap&lt;String, String&gt;();
          Map&lt;String,MethodInfoRO&gt; knownGoodMap = new LinkedHashMap&lt;String, MethodInfoRO&gt;();
  
          HexgenControllerClassFinder hexgenClassUtils = new HexgenControllerClassFinder();
          HttpClientRequests httpRequest = new HttpClientRequests();
          MethodParamsInfoRO methodParams ;
          LoadMethodDetailsInfoFromExcel methodDetails = new LoadMethodDetailsInfoFromExcel();
  
          Class cls;
          try {
              List controllerClassNames = hexgenClassUtils.findControllerClasses(HexgenWebAPITestConstants.BASE_PACKAGE);
              Iterator&lt;Class&gt; className = controllerClassNames.iterator();
              while(className.hasNext())
              {
                  Class obj = className.next(); 
                      cls = Class.forName(obj.getName());
                      Method[] methods = cls.getDeclaredMethods();
  
                      for (Method method : methods) {
  
                          RequestMapping requestMappingAnnotation = method.getAnnotation(RequestMapping.class); // gets the method which is maped with RequestMapping Annotation
  
                          if(requestMappingAnnotation !=null){ // requestMappingAnnotation if condition Starts 
                              PreAuthorize preAuthorizeAnnotation = method.getAnnotation(PreAuthorize.class); // gets the method which is maped with PreAuthorize Annotation
                              if(preAuthorizeAnnotation == null){ // preAuthorizeAnnotation if condition Starts 
                                  notSecuredMethodsList.add("Class : "+obj.getName()+"  Method : "+method.getName());
                              } // preAuthorizeAnnotation if condition Ends  
  
                              requestMappingValues = requestMappingAnnotation.value(); // to get the url value
                              RequestMethod[] requestMethods = requestMappingAnnotation.method(); // to get the request method type
                              requestingMethod = requestMethods[0].name();
                              methodsMap.put(requestMappingValues[0],method.getName());
                              //Following lines to get the request url and the requesting method type
                              urlDetailsMap.put(method.getName(), requestMappingValues[0]);
                              requestingMethodMap.put(method.getName(), requestingMethod);
  
                              Class[] parameterTypes = method.getParameterTypes();
                              LocalVariableTableParameterNameDiscoverer lcl = new LocalVariableTableParameterNameDiscoverer();
                              parametersDefinedForMethod = lcl.getParameterNames(method);
  
                              tempParamsList = new ArrayList();
                              for (int i=0;i&lt;parameterTypes.length;i++) { // check the parameter type and put them in to a ArrayList
                                  methodParams = new MethodParamsInfoRO();
                                  Class parameterType=parameterTypes[i];
                                  strClassNameToFix = parameterType.getName();
                                  strClassname =strClassNameToFix.replaceAll(HexgenWebAPITestConstants.PATTERN_TO_REMOVE,HexgenWebAPITestConstants.PATH_VARIABLE_TO_REPLACE).replaceAll(HexgenWebAPITestConstants.PATTERN_TO_REMOVE_SEMICOLON,HexgenWebAPITestConstants.PATH_VARIABLE_TO_REPLACE);
  
                                  methodParams.setDataType(strClassname);
                                  methodParams.setVariableDefined(parametersDefinedForMethod[i]);
  
                                      if(parameterType.isArray()){
                                          methodParams.setArray(true);
                                      }
                                       if(parameterType.isPrimitive()){
                                          methodParams.setPrimitive(true);
                                       } 
                                      //FIXME find some better way to address this problem
                                      if (strClassname.equals("java.math.BigDecimal")|| strClassname.equals("java.lang.String")|| strClassname.equals("boolean")) {
                                          methodParams.setPrimitive(true);
                                      }
                                      tempParamsList.add(methodParams);
                              }
                              paramsDetailsMap.put(method.getName(),tempParamsList);
                              //paramsList.add(tempParamsList);
  
                          }//requestMappingAnnotation if condition Ends 
  
                      }
  
              }
  
  
                 /** HTTPResponeCodes
                  * =================
                  * 1. 200 Response Successful 
                  * 
                  * 2. 400 Bad Request(The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.)
                  * 
                  * 3. 403 Forbidden
                  * 
                  * 4. 405 Method Not Allowed (The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.)
                  * 
                  * 5. 500 Internal Server Error(The server encountered an unexpected condition which prevented it from fulfilling the request.)
                  * 
                  */
  
          } catch (Exception ex) {
              ex.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>I have used apache-httpclient 4.0 for server communications. </p>