+ Start a Discussion
llisallisa 

Test class for mass upload of record in a custom object

Here i am trying to write a test class this class,which work is to upload large amount of records in a custom object.
Help me to write its test class please.

public class MassUpload{
    transient public Blob M_Body{get;set;}
    public String emailToalert{get;set;}
    public String retURL{get;set;}
    
    
    public MassUpload(){
    emailToalert='';
    retURL = ApexPages.currentPage().getParameters().get('retURL');
    }
    public void massUpload(){
        if(M_Body==null || M_Body.size()==0){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a file to attach.'));
        return;
        }
        try{
        List<String> csvFileLines=M_Body.toString().split('\n');
        csvFileLines.remove(0);
        if(csvFileLines!=null && csvFileLines.size()>0){
        MassUpload mass=new MassUpload(csvFileLines,emailToalert);
        Database.executeBatch(mass,26);
        }
        }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);
        }
    }
}
krishna chaitanya 35krishna chaitanya 35
Hi llisa,

please try this code:
@isTest
private class Test_MassUpload {

      static testMethod void testAttachments()
    {
        Account acc=new Account(Name='Acme Inc');
        insert acc;
        Test_MassUpload controller=new Test_MassUpload(new ApexPages.StandardController(acc));
 
        controller.fileName='Unit Test Attachment';
        controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:acc.id];
        System.assertEquals(1, attachments.size());
    }
}
K@SK@S
Hi LLisa,
see the below links
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html

 
llisallisa
Hello Krishna and kaek thanks for your reply.
it gives me an error like - "Constructor not defined: [MassUpload].&lt;Constructor&gt;(ApexPages.StandardController)"

To solve this issue i wrote the constructor like-

PageReference pageRef = Page.Mass_Upload;//VF page name
        pageRef.getParameters().put('id', String.valueOf(acc.Id)); //for page reference
        Test.setCurrentPage(pageRef);
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        MassUpload controller = new MassUpload(sc);
      //  test_MassUploadExhibitA controller=new test_MassUploadExhibitA(new ApexPages.StandardController(acc));

But still it gives me the same error.