function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce User 15salesforce User 15 

Test Class For this Controller class?

 can any one help me for writing test class for this..

public class vW_Contract_Attachment{

     public  vW_Contracts_and_Legal_Attachment__c vWDoc{get;set;}
      public Attachment attachContract {get;set;} 
       List<Account> acc;        
    
               public vW_Contract_Attachment(ApexPages.StandardController stdController) {
                            acc = [Select Id, Name From Account Limit 1];
                           
                       attachContract = new Attachment();
                 
                      vWDoc = (vW_Contracts_and_Legal_Attachment__c)stdController.getRecord();
                                                                  
                                                                    }
    
      public PageReference saveAttachment(){  
               vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
                           
                               insert vWDoc;
                               attachContract.ParentId =vWDoc.Id;
                               insert attachContract;
                   
                PageReference nextPage = new PageReference('/' + vWDoc.Id);
                  return nextPage;     
                   }
   }

Best Answer chosen by salesforce User 15
salesforce User 15salesforce User 15
Hey Bob_Buzzard finally got the solution by taking ypur advise here I am posting it 

@isTest
Private class Test_Contract_Attachments{
static testMethod void Test_Contract_Attachments()
    {
       
        Account acc=new Account(Name='Test Account',recordTypeId='012E00000001vtF');
        insert acc;
       
        Contracts_and_Legal_Attachment__c Doc = new  Contracts_and_Legal_Attachment__c();        
        Doc.Account_Name__c=acc.Id;
       
        ApexPages.StandardController sc = new ApexPages.StandardController(Doc);
        Contract_Attachment vW = new  Contract_Attachment(sc); 
       
        vW.attachContract.parentId=Doc.Id;
        vW.attachContract.Name='Test Document';
        vW.attachContract.Body=Blob.ValueOf('Test Data');         
        vW.saveAttachment();
        List<Attachment> attachments = [Select Id, Name from Attachment where Parent.Id=:vWDoc.Id Limit 1];
        System.assertEquals(1,attachments.size());
    }
    }

 conroller class

public class Contract_Attachment{

     public  Contracts_and_Legal_Attachment__c vWDoc{get;set;}    
     public Attachment attachContract {get;set;} 
    
     public Contract_Attachment(ApexPages.StandardController stdController) {  
                 
          Doc = (vContracts_and_Legal_Attachment__c)stdController.getRecord();         
          attachContract = new Attachment();                                                      
     }
    
     public PageReference saveAttachment(){                  
        
         if(Test.isRunningTest()){
             upsert Doc;
              
             attachContract.parentId = Doc.Id;
             insert attachContract;
             Attachment attch = [Select Id,bodyLength from Attachment WHERE Id = :attachContract.Id Limit 1];        
             Doc.Size__c = String.valueOf(attch.bodyLength/1024)+'KB';
             update Doc;
         }
         else{
             Doc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
             upsert Doc;
              
             attachContract.parentId = Doc.Id;
             insert attachContract;
         }
                      
         PageReference nextPage = new PageReference('/' + Doc.Id);
         return nextPage;                  
     }
}

All Answers

bob_buzzardbob_buzzard
Glad to help you.  You need to create an apex test class that does the following:

1. Creates an instance of vW_Contacts_and_Legal_Attachment__c and inserts it
2. Instantiates a standard controller, passing the sobject created in step 1 as the constructor argument.
3. Instantiates the vW_Contact_Attachment class, passing the standard controller created in step 2 as the constructor argument.
4. Creates an attachment and assigns it to the attachContract property
5. Execute the saveAttachment method and verifies it was saved correctly to the database.

As you have been able to write the apex code for the class, I'm sure you'll have no trouble with the test class. If you do be sure to post back.
James LoghryJames Loghry
In addition to bob_buzzard's excellent description, take a look at the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
salesforce User 15salesforce User 15
Hi bob_buzzard and james Loghry thank you very much. I am sorry my brain is not working today so please kindly help me in writing the code
 @isTest
Private class TestvW_Contract_Attachments{
static testMethod void TestvW_Contract_Attachments()
    {
       
        Account acc=new Account(Name='Test vWAccount',recordTypeId='012E00000001vtF');
        insert acc;
        Account acc1 = [select Id, Name from Account where Name='Test vWAccount'];
       
        vW_Contracts_and_Legal_Attachment__c vWDoc = new  vW_Contracts_and_Legal_Attachment__c();
        vWDoc.Account_Name__c=acc1.Id;
        vWDoc.File_Name__c='Test File';
        vWDoc.Size__c='50KB';
        vWDoc.Type__c='MSA';
        insert vWDoc;
         ApexPages.StandardController sc = new ApexPages.StandardController(vWDoc);
         vW_Contract_Attachment vW = new  vW_Contract_Attachment(sc);
        Attachment attachContract = new Attachment(); 
        attachContract.ParentId =vWDoc.Id;
        insert  attachContract;
        vW.saveAttachment();
 
      
    }
    }
bob_buzzardbob_buzzard
I assume that you are executing this test and its not behaving as expected.  Care to enlighten us as to how?
salesforce User 15salesforce User 15
Yes please
bob_buzzardbob_buzzard
You don't appear to have read my reply - I'm asking you waht the problem is, not offering to do anything for you.
salesforce User 15salesforce User 15
Oh I am sorry this reply is not for you my mistake thank you for your answer
salesforce User 15salesforce User 15
Hi Bob_Buzzard,

Yes I am trying to execute this test and I have changed the test code. I am stuck with this test class need some advise. I am getting a Null Pointer excetion caz my attachContract object is expecting a document to be attached but my test class is not providng that. can you please what's wrong wth my code below.

 public class vW_Contract_Attachment{

     public  vW_Contracts_and_Legal_Attachment__c vWDoc{get;set;}
      public Attachment attachContract {get;set;} 
       List<Account> acc;        
    
               public vW_Contract_Attachment(ApexPages.StandardController stdController) {
                            acc = [Select Id, Name From Account Limit 1];
                           
                       attachContract = new Attachment();
                 
                      vWDoc = (vW_Contracts_and_Legal_Attachment__c)stdController.getRecord();
                                                                  
                                                                    }
    
      public PageReference saveAttachment(){  
               vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
                           
                               insert vWDoc;
                               attachContract.ParentId =vWDoc.Id;
                               insert attachContract;
                   
                PageReference nextPage = new PageReference('/' + vWDoc.Id);
                  return nextPage;     
                   }
   }

@isTest
Private class TestvW_Contract_Attachments{
static testMethod void TestvW_Contract_Attachments()
    {
       
        Account acc=new Account(Name='Test vWAccount',recordTypeId='012E00000001vtF');
        insert acc;
        vW_Contracts_and_Legal_Attachment__c vWDoc = new  vW_Contracts_and_Legal_Attachment__c();
        vWDoc.Account_Name__c=acc.Id;
        insert vWDoc;
        ApexPages.StandardController sc = new ApexPages.StandardController(vWDoc);
        vW_Contract_Attachment vW = new  vW_Contract_Attachment(sc);  
        Attachment attach = new Attachment();
        attach.ParentId = vWDoc.Id;
        insert attach;
        vW.saveAttachment();
        vW_Contracts_and_Legal_Attachment__c vWDoc1 = [Select Id,Account_Name__c,Size__c,Type__c,File_Name__c from vW_Contracts_and_Legal_Attachment__c where Id=:vWDoc.Id];
        System.assertEquals(vW.saveAttachment(),null);
    }
    }

bob_buzzardbob_buzzard
You've missed out step 4 from my post above:

4. Creates an attachment and assigns it to the attachContract property

You have created an attachment, inserted it and not assigned it to the controller property.

Change your code from:

Attachment attach = new Attachment();
attach.ParentId = vWDoc.Id;
insert attach;
vW.saveAttachment();

to:

Attachment attach = new Attachment();
attach.ParentId = vWDoc.Id;
vW.attachContract=attach;
vW.saveAttachment();


salesforce User 15salesforce User 15
Hi Bob_Buzzard,

Actually I am getting error in "  vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB'; "  Because it is not getting attachContract.bodyLength from the test class. I don't know how to pass this from the test class. I am having a blob body but how to calucate the body length there and pass to the test class? Can you please suggest me..

 Main Controller class: vWDoc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB'
Test Class : Blob b = Blob.valueOf('Test Data');    
        vW.attachContract.parentId=vWDoc.Id;
        vW.attachContract.Name='Test Document';
        vW.attachContract.Body = b;

If I have to use any other thing instead of blob to get the body length of the attachment or I have to attache a file itself in the test class. Need Your help.
 
bob_buzzardbob_buzzard
Can you not just write a test value in tot he bodyLength property of the test contract?
salesforce User 15salesforce User 15
Yap I have tried that too in the test class vW.attachContract.bodyLength=1000; but it is throwing an error in the compliation itself

"Field is not writeable: Attachment.BodyLength". Is there any other way to attach a file to the custom object in the controller class and get the size of the file?
bob_buzzardbob_buzzard
Not that I know of.  You could use  the Test.isRunningTest to avoid that line - its not a great solution but will allow your test to complete.
salesforce User 15salesforce User 15
Even though I am keeping this Test.isRunningTest it is throwing the same error not passing the test. Any ways thank you very much for spending time for me.
bob_buzzardbob_buzzard
If you use test.isrunningtest to avoid that line, there's no way for it to fail with the same error.  Can you post your latest version of that method?
salesforce User 15salesforce User 15
Hey Bob_Buzzard finally got the solution by taking ypur advise here I am posting it 

@isTest
Private class Test_Contract_Attachments{
static testMethod void Test_Contract_Attachments()
    {
       
        Account acc=new Account(Name='Test Account',recordTypeId='012E00000001vtF');
        insert acc;
       
        Contracts_and_Legal_Attachment__c Doc = new  Contracts_and_Legal_Attachment__c();        
        Doc.Account_Name__c=acc.Id;
       
        ApexPages.StandardController sc = new ApexPages.StandardController(Doc);
        Contract_Attachment vW = new  Contract_Attachment(sc); 
       
        vW.attachContract.parentId=Doc.Id;
        vW.attachContract.Name='Test Document';
        vW.attachContract.Body=Blob.ValueOf('Test Data');         
        vW.saveAttachment();
        List<Attachment> attachments = [Select Id, Name from Attachment where Parent.Id=:vWDoc.Id Limit 1];
        System.assertEquals(1,attachments.size());
    }
    }

 conroller class

public class Contract_Attachment{

     public  Contracts_and_Legal_Attachment__c vWDoc{get;set;}    
     public Attachment attachContract {get;set;} 
    
     public Contract_Attachment(ApexPages.StandardController stdController) {  
                 
          Doc = (vContracts_and_Legal_Attachment__c)stdController.getRecord();         
          attachContract = new Attachment();                                                      
     }
    
     public PageReference saveAttachment(){                  
        
         if(Test.isRunningTest()){
             upsert Doc;
              
             attachContract.parentId = Doc.Id;
             insert attachContract;
             Attachment attch = [Select Id,bodyLength from Attachment WHERE Id = :attachContract.Id Limit 1];        
             Doc.Size__c = String.valueOf(attch.bodyLength/1024)+'KB';
             update Doc;
         }
         else{
             Doc.Size__c = String.valueOf(attachContract.bodyLength/1024)+'KB';
             upsert Doc;
              
             attachContract.parentId = Doc.Id;
             insert attachContract;
         }
                      
         PageReference nextPage = new PageReference('/' + Doc.Id);
         return nextPage;                  
     }
}
This was selected as the best answer
salesforce User 15salesforce User 15
Thank you very much