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
janardhan mjanardhan m 

How to write code coverage for this controller

public with sharing class pdfViewController {

public Document doc{
        get {
            if (doc== null) {
                doc= [SELECT id,Body, ContentType FROM Document WHERE ID ='0159E0000009NGe'];
            }
            return doc;
        }
        private set;
    }
    public String pdf {
        get {
            return EncodingUtil.Base64Encode(doc.body);
        }
    }
}
Best Answer chosen by janardhan m
Harini Manoharan 1Harini Manoharan 1
Hi Janardhan

If you need to access the existing data from Test class, you have to use the annotation (seeAllData=true).


@isTest(seeAllData=true)
public class Test_PdfViewCtrl{
   static testMethod void test_PDF(){
      pdfViewController  pd = new pdfViewController();
      Test.StartTest();
      
      Document doc = pd.doc;
      String pdf = pd.pdf;
      system.assertEquals('bssds', pdf); 
       
     Test.StopTest();
  
   } 

}

The above code will provide you 100% coverage.

Regards
Harini

All Answers

janardhan mjanardhan m
@isTest
Public Class Test_PdfViewCtrl{
   static testMethod void test_PDF(){
      pdfViewController pd = new pdfViewController();
       
       document doc = [SELECT id,Body, ContentType FROM Document limit 1];
       
       string pdf = EncodingUtil.Base64Encode(doc.body);
      
       system.assertEquals('bssds', pdf);
   } 

}

it shows 0 % coverage.
Harini Manoharan 1Harini Manoharan 1
Hi Janardhan

If you need to access the existing data from Test class, you have to use the annotation (seeAllData=true).


@isTest(seeAllData=true)
public class Test_PdfViewCtrl{
   static testMethod void test_PDF(){
      pdfViewController  pd = new pdfViewController();
      Test.StartTest();
      
      Document doc = pd.doc;
      String pdf = pd.pdf;
      system.assertEquals('bssds', pdf); 
       
     Test.StopTest();
  
   } 

}

The above code will provide you 100% coverage.

Regards
Harini
This was selected as the best answer
v varaprasadv varaprasad
Hi Janrdhan

First insert document record and then call it in test class like below.
 
@isTest(Seealldata = true)
Public Class Test_PdfViewCtrl{
   static testMethod void test_PDF(){
   
Document document;

document = new Document();
document.Body = Blob.valueOf('Some Text');
document.ContentType = 'application/pdf';
document.DeveloperName = 'my_document';
document.IsPublic = true;
document.Name = 'My Document';
document.FolderId = [select id,name from folder where name = 'My Personal Documents'].id;
insert document;

system.debug('======'+document);
      pdfViewController pd = new pdfViewController();
       
       document doc = [SELECT id,Body, ContentType FROM Document where id =: document.id];
       
       string pdf = EncodingUtil.Base64Encode(document.body);
      
       system.assertEquals('bssds', pdf);
   } 

}
plz check once and let me know.

Thanks
Varaprasad



 
janardhan mjanardhan m
Thanks varaprasad.