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
rajesh kumar 10rajesh kumar 10 

I am new to test Class and i have a problem with the test class for a trigger

This is my trigger 
trigger UpdatePrimaryApllicationAllocationSet on Primary_Application_Allocation__c (after insert,after delete) {

    list<id> lst = new list<id>();
    if(trigger.isInsert){
    for(Primary_Application_Allocation__c paa : trigger.new) {
        lst.add(paa.opportunity__c);
    }}
    if(trigger.isDelete){
        for(Primary_Application_Allocation__c paa : trigger.old) {
        lst.add(paa.opportunity__c);
        }
    }
   
    list<Opportunity> opplst = new list<Opportunity>();
    opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];

    if(trigger.isInsert) {
        for(Primary_Application_Allocation__c paa : trigger.new) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.Id) {
                    o.Primary_Application_Allocation_Set__c = true;
                }
            }
        }
        update opplst;
    }
   
    if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
        }
        update opplst;
    }
           
}


and down i have written a test class but it is totally mess cause i am unaware of test class can any one help me in doing this..

@isTest

public class UpdatePrimaryApllicationAllocationSet {

    static testMethod void UpdatePrimaryApllicationAllocationSet() {
          Test.startTest();
          Primary_Application_Allocation__c paa = new Primary_Application_Allocation__c(name='sample');
          opportunity o = [select name,oppotunity from opportunity where o.opportunity = paa.opportunity];
          insert paa;
          update o;
          Test.stopStart();
         
    }
   
}

magicforce9magicforce9
Hi,

Please see the test class below...You'll may to populate correct(valid/required)fields while inserting test data into Account/Opportunity/Custom Object

@isTest

public class UpdatePrimaryApllicationAllocationSet {

    static testMethod void insertPrimaryApllicationAllocationSet() {
          
        Account acc = new Account(name = 'Test Account');//Add any required fields
        insert acc;
        Opportunity opp = new Opportuntiy(Name = 'Test Opp', Amount = 10020, Stage = 'Some valid Stage name');//Add any required fields
        insert opp;
        Primary_Application_Allocation__c paa = new Primary_Application_Allocation__c(name='sample', Opportunity__c = opp.id);
        Test.startTest();
        insert paa;
        Test.stopStart();
        Opportunity updatedOpp = [Select Id, Primary_Application_Allocation_Set__c from Opportunity where id = opp.id];
        //Verifiying that is value is now changed to true due to after insert logic in trigger;
        System.assert(updatedOpp.Primary_Application_Allocation_Set__c);
         
    }
    static testMethod void deletePrimaryApllicationAllocationSet(){

        Account acc = new Account(name = 'Test Account');//Add any required fields
        insert acc;
        Opportunity opp = new Opportuntiy(Name = 'Test Opp', Amount = 10020, Stage = 'Some valid Stage name');//Add any required fields
        insert opp;
        Primary_Application_Allocation__c paa = new Primary_Application_Allocation__c(name='sample', Opportunity__c = opp.id);
        insert paa;
        Test.startTest();
        delete paa;
        Test.stopStart();
        Opportunity updatedOpp = [Select Id, Primary_Application_Allocation_Set__c from Opportunity where id = opp.id];
        //Verifiying that is value is now changed to false due to after delete logic in trigger;
        System.assert(!updatedOpp.Primary_Application_Allocation_Set__c);
          
    }

Bhawani SharmaBhawani Sharma
This link can help you to undestand the test methods.
http://simplyforce.blogspot.in/2013/06/test-methods-what-why-and-how.html