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
Leonardo Girgenti 5Leonardo Girgenti 5 

Unit test this trigger

i have this trigger, can anyone provide me with the relevant unit test?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trigger CreatePhotoDoc on Contact (before update) {

id fid=[select id from folder where name = 'Shared Documents'].id;
for (contact c : trigger.new){
string purl = c.photouploader__Image_URL__c;
if (Trigger.oldMap.get(c.Id).photouploader__Image_URL__c != purl  && purl !=null){
    string attid = purl.substring(purl.indexof('file=')+5,purl.length());
    attachment a = [select body,name,contenttype from attachment where id = :attid];
    Document doc;
    for (document d: [select id from document where name = :c.id])
        doc=d;
    if (doc==null)
        doc = new document();
    doc.Name = c.id;
    doc.Description = c.id + '/' + a.name;
    doc.FolderId = fid;//'00lj0000000qTMG';
    doc.Body = a.body;
    doc.type=a.name.substring(a.name.indexof('.')+1,a.name.length());
    doc.ispublic=true;
    upsert doc;
    string instance=null;
    string [] p = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
    if (p.size() == 3) Instance = p[0];
        else if (p.size() == 5) Instance = p[1];
    c.sdocs_image_url__c='<img src="https://c.' +instance +'.content.force.com/servlet/servlet.ImageServer?id=' + doc.id +'&oid='+UserInfo.getOrganizationId()+'" />';
    }
}
}
Best Answer chosen by Leonardo Girgenti 5
Tejpal KumawatTejpal Kumawat
Hello Leonardo Girgenti 5,

Find the test class :

@isTest(seeAllData = false)
public class CreatePhotoDocTest{
    // Unit test Method
    static testmethod void UnitTest() {
        Account acc = new Account(Name = 'Acc1');
        insert acc;
        
        Contact con = new Contact(FirstName = 'con1', LastName = 'test', AccountID = acc.Id);
        insert con;
        
        test.startTest();
            Blob b = Blob.valueOf('Test Data');  
            Attachment attachment = new Attachment();  
            attachment.ParentId = con.Id;  
            attachment.Name = 'Test Attachment for Parent';  
            attachment.Body = b;     
            insert(attachment);  
            
            con.photouploader__Image_URL__c = System.URL.getSalesforceBaseUrl().getHost()+ '/servlet/servlet.FileDownload?file='+attachment.id;
            update con;
        test.stopTest();
    }   
}

If this answers your question then hit Like and mark it as solution!
 

All Answers

pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
Tejpal KumawatTejpal Kumawat
Hello Leonardo Girgenti 5,

Find the test class :

@isTest(seeAllData = false)
public class CreatePhotoDocTest{
    // Unit test Method
    static testmethod void UnitTest() {
        Account acc = new Account(Name = 'Acc1');
        insert acc;
        
        Contact con = new Contact(FirstName = 'con1', LastName = 'test', AccountID = acc.Id);
        insert con;
        
        test.startTest();
            Blob b = Blob.valueOf('Test Data');  
            Attachment attachment = new Attachment();  
            attachment.ParentId = con.Id;  
            attachment.Name = 'Test Attachment for Parent';  
            attachment.Body = b;     
            insert(attachment);  
            
            con.photouploader__Image_URL__c = System.URL.getSalesforceBaseUrl().getHost()+ '/servlet/servlet.FileDownload?file='+attachment.id;
            update con;
        test.stopTest();
    }   
}

If this answers your question then hit Like and mark it as solution!
 
This was selected as the best answer
Abhi_TripathiAbhi_Tripathi
Hi Leonardo,

For the Test class, it not that too tough

Please go through this post you'll get what you want
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi
Salesforce Developer
Blog : http://abhithetechknight.blogspot.in/
 
Leonardo Girgenti 5Leonardo Girgenti 5
Hi there, Thank you for your help. Do I enter that as an Apex Class? Leo
Abhi_TripathiAbhi_Tripathi
Yes Leo, Test classes are written in same manner as Apex class.

Thanks
Abhi