Skip to main content
Douglas Ritter 님이 #Apex에 질문했습니다
I'm writing a test class for a trigger that is fired when our marketing cloud user updates an account. The test method passes, but the code coverage on the trigger remains 0%. Below is the code for the trigger and test class. In the test class, I'm using System.RunAs (line 46) to run it as the marketing cloud user. However, it doesn't appear to be activating the trigger. Any suggestions on how to correct it? 

TRIGGER

trigger Account on Account (after update){

Id userId = userinfo.getUserId();

Id RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Person Account' LIMIT 1].Id;

system.debug('PA RecordTypeId =' + ' ' + RecordTypeId);

List<user> mcUser = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' ];

system.debug('MC Users ='+ ' ' + mcUser);

if (RecordTypeId == RecordTypeId && userId == mcUser[0].id){

accountServices asp = new accountServices(trigger.newmap, trigger.oldmap);

asp.process();

system.debug ('The trigger fired');

}

}

TEST CLASS

@isTest

public class AccountTest{

static testMethod void validateAccount() {

Test.startTest();

Profile profile1 = [Select Id from Profile where name = 'System Administrator'];

System.debug('What is the profile id ' + profile1);

UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];

date tDate = date.today();

date uDate = Date.today().addDays(30);

User u = new User(

UserRoleId = portalRole.Id,

ProfileId = profile1.Id,

Username = 'testtermsconditions1234423@kaplan.com',

Alias = 'batman',

Email='testtermsconditions1234423@kaplan.com',

EmailEncodingKey='UTF-8',

Firstname='Bruce',

Lastname='Wayne',

LanguageLocaleKey='en_US',

LocaleSidKey='en_US',

TimeZoneSidKey='America/Chicago');

insert u;

System.debug ('Here is the user id ' + u.id);

User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];

system.debug('MC Users ='+ ' ' + u1);

System.runas(u1) {

//Create account

Account a = new Account(

Name = 'Test Account',

OwnerId = u.Id,

Program_Of_Interest__c = 'LSAT',

Expected_test_date__c = tDate,

Expected_Prep_Start_Date__c = tdate);

insert a;

System.debug('Here is the account id ' + a.id);

Test.stopTest();

}

}

}

 
답변 2개
  1. 2015년 7월 14일 오후 6:28

    Please try below code. I hope that will help you.

    @isTest(seeAllData=true)

    public class AccountTest

    {

    static testMethod void validateAccount()

    {

    Test.startTest();

    User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];

    List<RecordType> lstRc =[SELECT Id FROM RecordType WHERE Name = 'Person Account' LIMIT 1];

    system.debug('MC Users ='+ ' ' + u1);

    System.runas(u1)

    {

    //Create account

    Account a = new Account(

    Name = 'Test Account',

    OwnerId = u.Id,

    if(lstRc.size() >0 )

    {

    recordTypeId = lstRc[0].id;

    }

    Program_Of_Interest__c = 'LSAT',

    Expected_test_date__c = tDate,

    Expected_Prep_Start_Date__c = tdate);

    insert a;

    System.debug('Here is the account id ' + a.id);

    a.Name='Test Update';

    update a;

    Test.stopTest();

    }

    }

    }

    NOTE:- As per salesforce best pratice we should nt use seeAllData= true in test class. But in your Trigger you are using hard cord user name that is why i used that.

    For more information in test classes you can see below blog:-

    http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

    Please let us know if this will help you.

    Thanks,

    Amit Chaudhary

     
0/9000