+ Start a Discussion
rohit singh 92rohit singh 92 

i am getting an error when uploading a apex package

i am using a apex class with visual force page and trying to create a package and i am getting the following error .can anyone please help how to resolve the following issue ?


Upload Failed
No test methods found in the Apex code included in the package. At least 75% test coverage is required.

following is the apex class code :
@isTest
public class FileUploader 
{
    public string nameFile{get;set;}
    public string firstName{get;set;}
    public string lastName{get;set;}
    public string userAddress{get;set;}
    public string userCity{get;set;}
    public string userState{get;set;}
    public string userPostalCode{get;set;}
    public Object Phones;
    Contact newCon {get;set;}
    public Blob contentFile{get;set;}

    String[] filelines = new String[]{};
    List<Contact> accstoupload;
    
    /***This function reads the CSV file and inserts records into the Account object. ***/
    public Pagereference ReadFile()
    {
        try{
                //Convert the uploaded file which is in BLOB format into a string
                nameFile =blobToString( contentFile,'ISO-8859-1');
                
                //Now sepatate every row of the excel file
                filelines = nameFile.split('\n');
                
                //Iterate through every line and create a Account record for each row
                accstoupload = new List<Contact>();
                for (Integer i=1;i<filelines.size();i++)
                {
                    String[] inputvalues = new String[]{};
                    String PhoneList = '' ;
                   // my code
                   Test.startTest();
                  
                    inputvalues = filelines[i].split(',');
                             String licenseKey = '34f059d1-d6d3-4be0-a592-3ac254faa34c';
                    String MultiContactVerify =  'https://api.accurateappend.com/Services/V2/AppendPhone/Residential/'+ licenseKey + '/?';
                    firstName = inputvalues[0];
                    lastName = inputvalues[1];
                    userAddress = inputvalues[2];
                    userCity = inputvalues[4];
                    userState = inputvalues[5];
                    userPostalCode = inputvalues[6];
                    String MultiAccurateResponse = getContent(firstName,
                                                              lastName,
                                                              userAddress,
                                                              userCity,
                                                              userState,
                                                              userPostalCode
                                                              );
                    
                    JSON2Apex jtop = JSON2Apex.parse(MultiAccurateResponse);
                    
                    Phones = JSON.serialize(MultiAccurateResponse);
                    
                    JSON2Apex phoneListing = (JSON2Apex)JSON.deserialize(MultiAccurateResponse, JSON2Apex.class);
                    JSONParser parser = JSON.createParser(MultiAccurateResponse);
                    
                    parser.nextToken();
                    parser.nextValue();

                    if(parser.getCurrentName()=='Criteria')
                    {

                        for(Integer k = 0;k<phoneListing.Phones.size();k++)
                        {
                            PhoneList += phoneListing.Phones[k].PhoneNumber + ',';
                            //  PhoneList += k + ',';
                        }
                        
                        Contact a = new Contact();
                        a.FirstName  = inputvalues[0];
                        a.LastName = inputvalues[1];
                        a.Phone = PhoneList;
                        a.MailingStreet = useraddress;
                        a.MailingCity = usercity;
                        a.MailingState = userstate;
                        a.MailingCountry = '';
                        a.MailingPostalCode = userpostalcode;
                        a.accurateAppendVerified__c = true;
                        accstoupload.add(a);                
                  }else
                  {
                        Contact a = new Contact();
                        a.FirstName  = inputvalues[0];
                        a.LastName = inputvalues[1];
                        a.Phone = PhoneList;
                        a.MailingStreet = useraddress;
                        a.MailingCity = usercity;
                        a.MailingState = userstate;
                        a.MailingCountry = '';
                        a.MailingPostalCode = userpostalcode;
                        a.accurateAppendVerified__c = false;
                        accstoupload.add(a);                
                  
                  }
                  Test.stopTest();
            }
                    try{
                    test.startTest();

                              insert accstoupload;
                    test.stopTest();

                    }
                    catch (Exception e)
                    {
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
                        ApexPages.addMessage(errormsg);
                    }
        
         }
         catch(Exception e){
                 ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file'+e.getMessage());
                ApexPages.addMessage(errormsg);
         }       
        //Finally, insert the collected records
            
        return null;
    }

     public static  String getContent(String firstN,String LastN,String UserAddr,String UserCit,String UserStat,String Userpostalcod) {
         test.startTest();     
         String licenseKey = '34f059d1-d6d3-4be0-a592-3ac254faa34c';
         String MultiContactVerify =  'https://api.accurateappend.com/Services/V2/AppendPhone/Residential/'+ licenseKey + '/?';
         String MultiContactVerifyQueryString = 'firstname='+EncodingUtil.urlEncode(firstN, 'UTF-8')+
                   '&lastname='+EncodingUtil.urlEncode(LastN, 'UTF-8')+
                   '&address='+EncodingUtil.urlEncode(UserAddr, 'UTF-8')+
                   '&city='+EncodingUtil.urlEncode(UserCit, 'UTF-8')+
                   '&state='+EncodingUtil.urlEncode(UserStat, 'UTF-8')+
                   '&postalcode='+EncodingUtil.urlEncode(Userpostalcod, 'UTF-8')+
                   '&source=C;D;M' +
                   '&matchlevel=E1;E2;N1;N2';     
        String Multiurl =  MultiContactVerify + MultiContactVerifyQueryString;
        // return Multiurl ;
        //query string end
        // Instantiate a new http object
        Http h = new Http();
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(Multiurl);
        req.setMethod('GET');
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        test.stopTest();
        return res.getBody();
    }

    /*this function is used to verify single contact*/
    public Pagereference VerifySingleContact()
    {
        test.startTest();
        String SignleContactlicenseKey = '34f059d1-d6d3-4be0-a592-3ac254faa34c';
        String SignleContactVerify =  'https://api.accurateappend.com/Services/V2/AppendPhone/Residential/'+ SignleContactlicenseKey + '/?';
        String SignleContactVerifyQueryString = 'firstname='+EncodingUtil.urlEncode(firstname, 'UTF-8')+
               '&lastname='+EncodingUtil.urlEncode(lastname, 'UTF-8')+
               '&address='+EncodingUtil.urlEncode(useraddress, 'UTF-8')+
               '&city='+EncodingUtil.urlEncode(usercity, 'UTF-8')+
               '&state='+EncodingUtil.urlEncode(userstate, 'UTF-8')+
               '&postalcode='+EncodingUtil.urlEncode(userpostalcode, 'UTF-8')+
               '&source=C;D;M' +
               '&matchlevel=E1;E2;N1;N2';
        String url =  SignleContactVerify  + SignleContactVerifyQueryString ;
        String AccurateResponse;
        String log;
        //query string end
        accstoupload = new List<Contact>();
    
        // Instantiate a new http object
        Http h = new Http();
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        //return res.getBody();
        AccurateResponse = res.getBody();
        JSONParser parser = JSON.createParser(AccurateResponse);
        JSON2Apex jtop = JSON2Apex.parse(AccurateResponse);
        parser.nextToken();
        parser.nextValue();
        
        if(parser.getCurrentName()=='Criteria')
        {
            String PhoneList = '' ;
           // my code
            Phones = JSON.serialize(jtop.Phones);
            JSON2Apex phoneListing = (JSON2Apex)JSON.deserialize(AccurateResponse, JSON2Apex.class);
            for(Integer k = 0;k<phoneListing.Phones.size();k++)
            {
                PhoneList +=    '('+phoneListing.Phones[k].AreaCode+')'+ phoneListing.Phones[k].PhoneNumber + ',';
            }
           // my code           
                Account acc = new Account(Name = 'Robert L Knox');
                Contact a = new Contact();
                a.FirstName = firstname ;
                a.LastName = lastname ;
                a.AccountId = a.Id;
                a.Phone = PhoneList;
                a.MailingStreet = useraddress;
                a.MailingCity = usercity;
                a.MailingState = userstate;
                a.MailingCountry = '';
                a.MailingPostalCode = userpostalcode;
                a.accurateAppendVerified__c = true;
                accstoupload.add(a);
            
            try{
              insert accstoupload;
            }
            catch (Exception e)
            {
                ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
                ApexPages.addMessage(errormsg);
            }
        }else
        {
            Account acc = new Account(Name = 'Robert L Knox');
            Contact a = new Contact();
            a.FirstName = firstname ;
            a.LastName = lastname ;
            a.AccountId = a.Id;
            a.accurateAppendVerified__c = false;
            accstoupload.add(a);
        }
        
        test.stopTest();        
        return null;
    }
   
   /**** This function sends back to the visualforce page the list of account records that were inserted ****/ 
    public List<Contact> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }  
    
    /**
         This function convers the input CSV file in BLOB format into a string
        @param input    Blob data representing correct string in @inCharset encoding
        @param inCharset    encoding of the Blob data (for example 'ISO 8859-1')
     */
    public static String blobToString(Blob input, String inCharset){
    test.startTest();
        String hex = EncodingUtil.convertToHex(input);
        System.assertEquals(0, hex.length() & 1);
        final Integer bytesCount = hex.length() >> 1;
        String[] bytes = new String[bytesCount];
        for(Integer i = 0; i < bytesCount; ++i)
            bytes[i] =  hex.mid(i << 1, 2);
        test.stopTest();
        return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }         
}
Pankaj_GanwaniPankaj_Ganwani
Hi Rohit,

It is a best practice to create a separate test class for each controller instead of writting test methods inside of the class. It seems you have placed the test annotations in main class. Just remove them and write down a separate one and add it along with your main class while creating package. Make sure the code coverage of the main class should be more than 75%.

Thanks,
Pankaj
rohit singh 92rohit singh 92
and can u please u please guide me how to make code coverage 75% ?
Pankaj_GanwaniPankaj_Ganwani
Hi Rohit,

Please refer the below mentioned link for creating the test class:

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
rohit singh 92rohit singh 92
can u please tell me about how i can create test class for this main apex class ?