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
gb__gb__ 

Apex Test Class assistance needed

Hi all,

I'm struggling to get code coverage on my class.  I've never written a test class for a Controller, only triggers, so I'm a bit lost.
Any help would be greatly appreciated.
 

Thanks,

Greg

Class

public with sharing class ShowAttachments_Controller {
private List<Id> photoIds;
private ApexPages.standardController controller;
private List<Id> chatterphotoIds;
private Acquisition_Report__c acquisition;
public ShowAttachments_Controller(ApexPages.StandardController controller) { 
this.controller = controller;         
 this.acquisition = (Acquisition_Report__c)controller.getRecord();
}

public List<Id> photosfromchatter {
get {
if(chatterphotoIds == null) {
chatterphotoIds = new List<Id>();
for(ContentVersion cnt : [select Id from ContentVersion ]) {
chatterphotoIds.Add(cnt.Id);
}
}

return chatterphotoIds;
}
} 

public List<Id> photos {
        get {
            if(photoIds == null) {
                photoIds = new List<Id>();
                for(Attachment att : [select Id from Attachment where ParentId = :acquisition.Id]) {
                    photoIds.Add(att.Id);
                }
            }
                             
            return photoIds;
        }
    }
     
    public Integer totalPhotos {
        get {
            return photos.size();
        }
    }

}

Test Class
@isTest
private class ShowAttachments_ControllerTest {
     
    static testMethod void validateShowAttachments_ControllerTest(){
   
{
        Account acc=new Account(Name='Acme Inc');
        insert acc;
       test.startTest();
        Attachment attach=new Attachment();   	
    	attach.Name='Unit Test Attachment';
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
    	
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:acc.id];
        System.assertEquals(1, attachments.size());
    	String myString = 'StringToBlob';
		Blob myBlob = Blob.valueof(myString);
		System.assertEquals('StringToBlob', myBlob.toString());
    }
        test.stopTest();
}        
}

 
Best Answer chosen by gb__
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code bellow changing it as needed:
 
@isTest
private class ShowAttachments_ControllerTest {
     
    static testMethod void validateShowAttachments_ControllerTest(){
 
        Account acc=new Account(Name='Acme Inc');
        insert acc;
		
		test.startTest();
        Attachment attach=new Attachment();   	
    	attach.Name='Unit Test Attachment';
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
    	
		// Here you have to create a Content as you need and with the proper RecordType if needed
		ContentVersion content = new ContentVersion(); 
		content.ContentURL='<a target="_blank" href="http://www.google.com/';">http://www.google.com/';</a> 
		content.Title ='Google.com';
		content.RecordTypeId = ContentRT.Id; // if needed
		insert content; 			
        
        List<Attachment> attachments= [SELECT id, Name FROM Attachment WHERE parent.Id =: acc.Id];
        System.assertEquals(1, attachments.size());
    	String myString = 'StringToBlob';
		Blob myBlob = Blob.valueof(myString);
		System.assertEquals('StringToBlob', myBlob.toString());
		
		Acquisition_Report__c  acquisition = new Acquisition_Report__c();
		// set here all required fields with a value before insert 
		
		insert aquisition;
		
		// Instantiate contollers
		ApexPages.StandardController stdController = new ApexPages.StandardController(acquisition);
		ShowAttachments_Controller  controller = new ShowAttachments_Controller(stdController);
		
		Integer totalPhotos = controller.totalPhotos;
		System.assert(totalPhotos == 1);
		
		List<Id> photos = controller.photos;
		// Assertion if needed	
		List<Id> photosfromchatter  = controller.photosfromchatter;
		// Assertion if needed
		
        test.stopTest();
	}        
}
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code bellow changing it as needed:
 
@isTest
private class ShowAttachments_ControllerTest {
     
    static testMethod void validateShowAttachments_ControllerTest(){
 
        Account acc=new Account(Name='Acme Inc');
        insert acc;
		
		test.startTest();
        Attachment attach=new Attachment();   	
    	attach.Name='Unit Test Attachment';
    	Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    	attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
    	
		// Here you have to create a Content as you need and with the proper RecordType if needed
		ContentVersion content = new ContentVersion(); 
		content.ContentURL='<a target="_blank" href="http://www.google.com/';">http://www.google.com/';</a> 
		content.Title ='Google.com';
		content.RecordTypeId = ContentRT.Id; // if needed
		insert content; 			
        
        List<Attachment> attachments= [SELECT id, Name FROM Attachment WHERE parent.Id =: acc.Id];
        System.assertEquals(1, attachments.size());
    	String myString = 'StringToBlob';
		Blob myBlob = Blob.valueof(myString);
		System.assertEquals('StringToBlob', myBlob.toString());
		
		Acquisition_Report__c  acquisition = new Acquisition_Report__c();
		// set here all required fields with a value before insert 
		
		insert aquisition;
		
		// Instantiate contollers
		ApexPages.StandardController stdController = new ApexPages.StandardController(acquisition);
		ShowAttachments_Controller  controller = new ShowAttachments_Controller(stdController);
		
		Integer totalPhotos = controller.totalPhotos;
		System.assert(totalPhotos == 1);
		
		List<Id> photos = controller.photos;
		// Assertion if needed	
		List<Id> photosfromchatter  = controller.photosfromchatter;
		// Assertion if needed
		
        test.stopTest();
	}        
}
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
gb__gb__
Thanks Zuinglio Lopes Ribeiro Júnior, it worked!
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Glad to have helped!