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
Swaroopa Akula 10Swaroopa Akula 10 

Test class code coverage- covering only 50%

Apex class:

public class OpportunityTriggerHandlerverifiedIO {
    public static void handleClosedWonOpportunities(List<Opportunity> newOpportunities) {
        Set<Id> oppIds = new Set<Id>();
        List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
        List<Opportunity> opportunity = new List<Opportunity>();

        for (Opportunity opp : newOpportunities) {
            if (opp.StageName == 'Closed Won') {
                oppIds.add(opp.Id);
                opportunity.add(opp);
            }
        }

        List<APXT_Redlining__Contract_Agreement__c> con = [SELECT Id, Opportunity__c, APXT_Redlining__Status__c, Verified_Signed_IO__c FROM APXT_Redlining__Contract_Agreement__c WHERE Opportunity__c IN :oppIds];

        if (con.size() > 0) {
            System.debug('Number of Contract Agreements: ' + con.size());
            for (APXT_Redlining__Contract_Agreement__c c : con) {
                if (c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c == false) {
                    oppWithContractAgreement.add(c);
                    System.debug('-----------' + oppWithContractAgreement.size());
                }
            }
        }

        for (Opportunity opps : opportunity) {
            if (oppWithContractAgreement != null && oppWithContractAgreement.size() > 0) {
                System.debug('Number of Opportunities with Contract Agreements: ' + oppWithContractAgreement.size());
                opps.addError('Third party paper must be approved by Legal, please submit a “Legal Case');
            }

            if (con.size() == 0) {
                opps.addError('Closed Won Opportunity must have a contract agreement.');
            }
        }
    }
}


Test class:

@isTest
private class OpportunityTriggerHandlerverifiedIOTest1 {
    @isTest
    static void testHandleClosedWonOpportunities() {
        // Create test data
        Opportunity testOpportunity1 = new Opportunity(
            Name = 'Test Opportunity1',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 30'
        );
        insert testOpportunity1;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create an active contract agreement without verified signed IO
        APXT_Redlining__Contract_Agreement__c contract1 = new APXT_Redlining__Contract_Agreement__c(
            Opportunity__c = testOpportunity1.Id,
            APXT_Redlining__Status__c = 'Active',
            Verified_Signed_IO__c = false,
            X3rd_Party_Paper__c = true
        );
        insert contract1;

        // Test: Contract agreement with Active status, but Verified_Signed_IO__c = false, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for contract agreement without verified signed IO
            System.assertEquals('Third party paper must be approved by Legal, please submit a “Legal Case”.', e.getMessage());
        }
        Test.stopTest();

        // Update the Contract Agreement to meet the conditions
        contract1.Verified_Signed_IO__c = true;
        update contract1;

        // Test: Contract agreement with Active status and Verified_Signed_IO__c = true, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        update testOpportunity1;
        Test.stopTest();

        // Test: No contract agreement, StageName = Closed Lost
        testOpportunity1.StageName = 'Closed Lost';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create another opportunity with a different Billing Term and without contract agreement
        Opportunity testOpportunity2 = new Opportunity(
            Name = 'Test Opportunity2',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 15'
        );
        insert testOpportunity2;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity2.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity2;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();
    }
}


Code covering only 50%. Can someone help me how to achieve 100% code coverge
SwethaSwetha (Salesforce Developers) 
HI Swaroopa
Can you highlight which lines of your code are uncovered? Thanks
Swaroopa Akula 10Swaroopa Akula 10
public class OpportunityTriggerHandlerverifiedIO {
    public static void handleClosedWonOpportunities(List<Opportunity> newOpportunities) {
        Set<Id> oppIds = new Set<Id>();
        List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
        List<Opportunity> opportunity = new List<Opportunity>();

        for (Opportunity opp : newOpportunities) {
            if (opp.StageName == 'Closed Won') {
                oppIds.add(opp.Id);
                opportunity.add(opp);

            }
        }

        List<APXT_Redlining__Contract_Agreement__c> con = [SELECT Id, Opportunity__c, APXT_Redlining__Status__c, Verified_Signed_IO__c FROM APXT_Redlining__Contract_Agreement__c WHERE Opportunity__c IN :oppIds];

        if (con.size() > 0) {
            System.debug('Number of Contract Agreements: ' + con.size());
            for (APXT_Redlining__Contract_Agreement__c c : con) {
                if (c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c == false) {
                    oppWithContractAgreement.add(c);

                    System.debug('-----------' + oppWithContractAgreement.size());
                }
            }
        }

        for (Opportunity opps : opportunity) {
            if (oppWithContractAgreement != null && oppWithContractAgreement.size() > 0) {
                System.debug('Number of Opportunities with Contract Agreements: ' + oppWithContractAgreement.size());
                opps.addError('Third party paper must be approved by Legal, please submit a “Legal Case');
            }


            if (con.size() == 0) {
                opps.addError('Closed Won Opportunity must have a contract agreement.');

            }
        }
    }
}


Underlined code is not covered. Thanks in advance!
Swaroopa Akula 10Swaroopa Akula 10
@swetha can you check the underlined code and help me out?