System.runAs Test class

 @isTest

public class OpportunityTriggerHelperTest {

    @isTest

    static void setupTestData(){

        User uc;

        Profile p = [SELECT Id,Name FROM Profile WHERE Name='System Administrator'];

        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() LIMIT 1];

        System.runAs(thisUser) {

            

            uc = new User(email = 'test@test.com',

                          profileid = P.Id,

                          UserName = 'ad@test.test.com',

                          Alias = 'admc',

                          TimeZoneSidKey = 'America/New_York',

                          EmailEncodingKey = 'ISO-8859-1',

                          LocaleSidKey = 'en_US',

                          LanguageLocaleKey = 'en_US',

                          //PortalRole = 'Manager',

                          FirstName = 'Student',

                          LastName = 'Climber');

            insert uc;

        }

        

        Id conRecordTypeId = TestClassUtil.getRecordTypeId('Contact','OEM');

        Id accRecordTypeId = TestClassUtil.getRecordTypeId('Account','OEM');

        // 

        Contact con = new Contact();

        con.LastName = 'test';

        con.RecordTypeId = conRecordTypeId;//TestClassUtil.getRecordTypeId('Contact ','OEM');

        

        con.MailingStreet = 'Ring Road';

        con.MailingCity = 'Pune';

        con.MailingPostalCode = '451010';

        con.MailingState = 'maharashtra';

        con.MailingCountry = 'India';

        // con.AccountId = accRec.Id;

        Insert con;

        

        

        Account accRec = new Account();

        accRec.Name = 'test acc';

        accRec.RecordTypeId = accRecordTypeId;

        accRec.Distribution_Channel__c = 'Domestic';

        accRec.CurrencyIsoCode = 'INR';

       accRec.Sales_Region__c = 'AAW';

        accRec.Phone = '22222';

        accRec.Mobile__c = '22222';

        accRec.Email1__c ='test@gmail.com';

        accRec.Fax= '22222';

        accRec.Website= 'www.test.com';

        accRec.PAN_Number__c='1111111';

        //accRec.Exim_Code__c ='11111';

        accRec.VAT_Number__c = '2222';

        accRec.SAP_Customer_Code__c ='3456';

        accRec.Sales_Region_Type__c = 'Domestic';

        accRec.Price_Book__c = Test.getStandardPricebookId();

        Insert accRec;

        System.debug('con: '+con);

        System.debug('accRec: '+accRec);

        

           Opportunity opportunity = TestClassUtil.createOpportunity(accRec.Id, 'test', 'Customer Readiness', con.Id);

        try{

        opportunity.StageName ='Technical Negotiation Completed';

        update opportunity; 

        Non_Standard_Requirement__c nonStandardReq=new Non_Standard_Requirement__c();

        nonStandardReq.Query_Type__c='Technical';

        nonStandardReq.Status__c ='Open';

        nonStandardReq.Opportunity__c=opportunity.id;

        insert nonStandardReq;

        List<Opportunity> lstOpportunities=[SELECT ID,StageName FROM Opportunity];

        Map<Id, Opportunity> opportunityOldMap=new Map<Id, Opportunity>([SELECT ID,StageName FROM Opportunity]);

        OpportunityTriggerHelper.beforeUpdate(lstOpportunities,opportunityOldMap);

        }catch(exception e){

            system.debug('test');

        }

        

        

        

        Non_Standard_Requirement__c nonStandardReq1=new Non_Standard_Requirement__c();

        nonStandardReq1.Query_Type__c ='Commercial';

        nonStandardReq1.Status__c ='Open';

        nonStandardReq1.Opportunity__c=opportunity.id;

        insert  nonStandardReq1;

        List<Opportunity> lstOpportunities1=[SELECT ID,StageName FROM Opportunity];

        Map<Id, Opportunity> opportunityOldMap1=new Map<Id, Opportunity>([SELECT ID,StageName FROM Opportunity]);

        OpportunityTriggerHelper.beforeUpdate(lstOpportunities1,opportunityOldMap1);

            

        try{

            opportunity.StageName='Closed Lost';

            update opportunity;

            Order_Loss_New__c ol = new Order_Loss_New__c();

            ol.Information_Make__c = 'Botheven';

            ol.Account__c = accRec.id;

            ol.Information_Qty__c =4;

            ol.Tapeline_Die_width__c= 4;

            ol.Loss_Reason_1__c = 'Loyalty to competitor';

            ol.Opportunity__c = opportunity.Id;

            insert ol;

            List<Opportunity> lstOpportunities2=[SELECT ID,StageName FROM Opportunity];

            Map<Id, Opportunity> opportunityOldMap2=new Map<Id, Opportunity>([SELECT ID,StageName FROM Opportunity]);

            OpportunityTriggerHelper.beforeUpdate(lstOpportunities2,opportunityOldMap2);

        }catch(exception e){

            system.debug('Error ');

        }

        

        try{

            opportunity.StageName='Closed Won';

            update opportunity;

            Order_Loss_New__c ol = new Order_Loss_New__c();

            ol.Information_Make__c = 'Botheven';

            ol.Account__c = accRec.id;

            ol.Information_Qty__c =4;

            ol.Tapeline_Die_width__c= 4;

            ol.Loss_Reason_1__c = 'Loyalty to competitor';

            ol.Opportunity__c = opportunity.Id;

            insert ol;

            List<Opportunity> lstOpportunities2=[SELECT ID,StageName FROM Opportunity];

            Map<Id, Opportunity> opportunityOldMap2=new Map<Id, Opportunity>([SELECT ID,StageName FROM Opportunity]);

            OpportunityTriggerHelper.beforeUpdate(lstOpportunities2,opportunityOldMap2);

        }catch(exception e){

            system.debug('Error ');

        }

    }

    

    @isTest static void contactHandler(){  

        Test.startTest(); 

        OpportunityTriggerHelper.codeCoverage();      

        Test.stopTest();

    } 

}

Comments

Popular posts from this blog

Custom List View Button In Salesforce

Get Record Id RecordId in LWC

PDF LWC (Link for pdf liabrary)