You need to sign in to do that
Don't have an account?

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()+'" />';
}
}
}
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()+'" />';
}
}
}
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
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/
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!
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/
Thanks
Abhi