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

Test class on Trigger.
I have wrote a trigger, this trigger will initiate only for the particular profile.so I wrote trigger like this:
But its not covering the code which is inside the if condition. how to make the if condition "true" in test class?
trigger TriggerOnAttachments on Attachment (after insert) {
Profile ProfileName = [select Name from profile where id = :userinfo.getProfileId()];
if(ProfileName.Name=='AD - Integration User'){
List<Id> parentIds=new List<Id>();
for (Attachment att : trigger.new) {
parentIds.add(att.ParentId);
}
List<Material_Sales_Data2__c> msd2 = [select id, Attachment_Exist__c from Material_Sales_Data2__c where id in :parentIds];
if(msd2.size()>0) {
for (Material_Sales_Data2__c msd : msd2) {
msd.Attachment_Exist__c = true;
}
update msd2;
} }
}
Test Class
@isTest
private class TriggerOnAttachmentsTest {
static testMethod void myUnitTest() {
Material_Sales_Data2__c msd2=new Material_Sales_Data2__c(Attachment_Exist__c=true,Material_Number__c='782222',Dist_Channel_Code__c='31',Record_Key__c='Recordkey01',Sales_Org_Code__c='1000');
insert msd2;
List<Material_Sales_Data2__c> msd2obj=[select id, name from Material_Sales_Data2__c where id=:msd2.id];
System.assertEquals(true, msd2.Attachment_Exist__c);
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=msd2.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:msd2.id];
System.assertEquals(1, attachments.size());
}
}
try with this code
@isTest
private class TriggerOnAttachmentsTest {
static testMethod void myUnitTest() {
Profile p = [SELECT Id FROM Profile WHERE Name='AD - Integration User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
System.runAs(u) {
Material_Sales_Data2__c msd2=new Material_Sales_Data2__c(Attachment_Exist__c=true,Material_Number__c='782222',Dist_Channel_Code__c='31',Record_Key__c='Recordkey01',Sales_Org_Code__c='1000');
insert msd2;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=msd2.id;
insert attach;
}
}
}
All Answers
You'll need to use the System.runAs method and execute the code that fires the trigger as a user with that profile.
There's an example in the docs at:
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_testing_tools_runas.htm|SkinName=webhelp
try with this code
@isTest
private class TriggerOnAttachmentsTest {
static testMethod void myUnitTest() {
Profile p = [SELECT Id FROM Profile WHERE Name='AD - Integration User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
System.runAs(u) {
Material_Sales_Data2__c msd2=new Material_Sales_Data2__c(Attachment_Exist__c=true,Material_Number__c='782222',Dist_Channel_Code__c='31',Record_Key__c='Recordkey01',Sales_Org_Code__c='1000');
insert msd2;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=msd2.id;
insert attach;
}
}
}
Hi,
You have to create the Profile manually with name “AD - Integration User” inside the org. Now try the below test method as reference:
@isTest
private class testTriggerinsert_Contact_A
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
Profile p = [select id from profile where name='AD - Integration User'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;
system.runas(u1)
{
Material_Sales_Data2__c m=new Material_Sales_Data2__c(name='test',Attachment_Exist__c=false);
insert m;
Attachment attachment = new Attachment(Name='An attachment',body=blob.valueof('b'),parentId=m.Id);
insert attachment;
}
}
}
Thanks, Its working fine. But the problem is i am having "system Administrator" profile. I need to give condition for "AD - Integration User'" profile. If i run the code with this
Profile p = [SELECT Id FROM Profile WHERE Name='AD - Integration User'];
It shows failure message:"System.QueryException: List has no rows for assignment to SObject".
If i give like this
Profile p = [SELECT Id FROM Profile WHERE Name='system Administrator'];
Its working fine.
How can i run the test class for other profile?
Do you have that profile defined in your system?
Sorry, it’s not available in development environment. I have to run this code in UAT. Thanks for your guidance.