+ Start a Discussion
Veerendar AellaVeerendar Aella 

apex error: testMethod can only be used on methods of a top level type

Hi All,

I am getting above error in my test class at 16 and 40, below is my code.
Please help.

Apex class:

public class ImportCSV1 {

    public Blob csvFileBody{get;set;}
    public  string csvAsString{get;set;}
    public  String[] csvFileLines{get;set;}
    public List<account> acclist{get;set;}
    public ImportCSV1(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
  }
  
  public  void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Phone = csvRecordData[2];
               accObj.Industry = csvRecordData[3]; 
               accObj.Type = csvRecordData[4];
               accObj.Description = csvRecordData[5];
               accObj.Website = csvRecordData[6];
               accObj.Site = csvRecordData[7];
               accObj.Ownership = csvRecordData[8];
               accObj.Sic = csvRecordData[9];
               acclist.add(accObj);   
           }
        insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}


Apex test class:

@istest
public class Test1 {
   
   public class ImportDataFromCSVControllerTest {

       public String str = 'Name,AccountNumber,Phone,Industry\n Esha Patharabe,10001,989632145,Banking\n Trupti Nimje,10002,875469856,Banking';      

        String csvAsString;

      String[] csvFileLines;

      Blob csvFileBody;

 

    public  testmethod void testfileupload(){

        Test.startTest();      

        csvFileBody = Blob.valueOf(str);

         String csvAsString = csvFileBody.toString();

        csvFileLines = csvAsString.split('\n');

 

        ImportCSV1 importData = new ImportCSV1();

        importData.csvFileBody = csvFileBody;

        importData.importCSVFile();

        Test.stopTest();

    }

 

    public  testmethod void testfileuploadNegative(){

        Test.startTest();      

        csvFileBody = Blob.valueOf(str);

        String csvAsString = csvFileBody.toString();

        csvFileLines = csvAsString.split('\n');

 

        ImportCSV1 importData = new ImportCSV1();

        importData.importCSVFile();

        Test.stopTest();

    }

}
}
Best Answer chosen by Veerendar Aella
Raj VakatiRaj Vakati
Here is the code
 
@istest
public class ImportCSV1Test {
    static testMethod void testName() {
        
        Test.startTest(); 
        String str = 'Name,AccountNumber,Phone,Industry\n Esha Patharabe,10001,989632145,Banking\n Trupti Nimje,10002,875469856,Banking';      
        
        String csvAsString;
        
        String[] csvFileLines;
        
        Blob csvFileBody;
        
        csvFileBody = Blob.valueOf(str);
        
        csvAsString = csvFileBody.toString();
        
        csvFileLines = csvAsString.split('\n');
        
        
        
        ImportCSV1 importData = new ImportCSV1();
        
        importData.csvFileBody = csvFileBody;
        
        importData.importCSVFile();
        
        Test.stopTest();
        
    }
    
    
    
    static testMethod void testName1() {
        
        
        Test.startTest();  
        String str = 'Name,AccountNumber,Phone,Industry\n Esha Patharabe,10001,989632145,Banking\n Trupti Nimje,10002,875469856,Banking\n Trupti Nimje,10002,875469856,Banking\n asdasd.com\n asdasd.com\n asdasd.com\n asdasd.com';      
        
        String csvAsString;
        
        String[] csvFileLines;
        
        Blob csvFileBody;		
        
        csvFileBody = Blob.valueOf(str);
        
        csvAsString = csvFileBody.toString();
        
        csvFileLines = csvAsString.split('\n');
        
        
        
        ImportCSV1 importData = new ImportCSV1();
        
        importData.importCSVFile();
        
        Test.stopTest();
        
    }
    
    
}

 

All Answers

Raj VakatiRaj Vakati
Here is the code
 
@istest
public class ImportCSV1Test {
    static testMethod void testName() {
        
        Test.startTest(); 
        String str = 'Name,AccountNumber,Phone,Industry\n Esha Patharabe,10001,989632145,Banking\n Trupti Nimje,10002,875469856,Banking';      
        
        String csvAsString;
        
        String[] csvFileLines;
        
        Blob csvFileBody;
        
        csvFileBody = Blob.valueOf(str);
        
        csvAsString = csvFileBody.toString();
        
        csvFileLines = csvAsString.split('\n');
        
        
        
        ImportCSV1 importData = new ImportCSV1();
        
        importData.csvFileBody = csvFileBody;
        
        importData.importCSVFile();
        
        Test.stopTest();
        
    }
    
    
    
    static testMethod void testName1() {
        
        
        Test.startTest();  
        String str = 'Name,AccountNumber,Phone,Industry\n Esha Patharabe,10001,989632145,Banking\n Trupti Nimje,10002,875469856,Banking\n Trupti Nimje,10002,875469856,Banking\n asdasd.com\n asdasd.com\n asdasd.com\n asdasd.com';      
        
        String csvAsString;
        
        String[] csvFileLines;
        
        Blob csvFileBody;		
        
        csvFileBody = Blob.valueOf(str);
        
        csvAsString = csvFileBody.toString();
        
        csvFileLines = csvAsString.split('\n');
        
        
        
        ImportCSV1 importData = new ImportCSV1();
        
        importData.importCSVFile();
        
        Test.stopTest();
        
    }
    
    
}

 
This was selected as the best answer
Veerendar AellaVeerendar Aella
Worked fine..Thanks Raj.