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
Ankit Singh 6Ankit Singh 6 

Code coverage showing 0 %

Hi All

Below is the code for my trigger and test class but the code coverage showing 0 for me. Please let me know where I am going wrong

Trigger


trigger stageChangetest on Opportunity (before update) {
    List<string> accountNames = new List<string>();
    for (Opportunity o : trigger.new) {
        if(trigger.isupdate && o.Stagename == '7. Closed/Won'){
         accountNames.add(o.AccountId);
        }
    }
    AggregateResult[] results = [select Account.Id, MAX(cas_LastConnect__c) from Contact where Account.Id in :accountNames and cas_LastConnect__c != null group by Account.Id ];
    Map<String, Datetime> account_connectDate = new Map<String, Datetime>();
    for(AggregateResult ar : results){
        account_connectDate.put(String.valueof( ar.get('Id')), Datetime.valueof( ar.get('expr0')));
    }
    
    Datetime today = Date.today().addMonths(-6);
    List<Opportunity> opportunities = new List<Opportunity>();
    for (Opportunity o : trigger.new) {
        if(trigger.isupdate && o.Stagename == '7. Closed/Won' && account_connectDate.containsKey(o.AccountId)){
            Datetime lastConnectDate = account_connectDate.get(o.AccountId);
            system.debug(logginglevel.info,today);
            system.debug(logginglevel.info,lastConnectDate);
            if(lastConnectDate > today){
                o.ConnectAndSell_bookings_contribution__c = o.Amount;
            }
        }
    }
}

Test Class


@isTest(seeAllData=false)
private class cas_ClosedAmountTest {
    /** Test with an existing opportunity **/
    static testMethod void testWithExistingContact() {
    //init();
    Test.startTest();
    Account acc = new Account();
    acc.Name = 'test account';
    acc.Phone = '123214';
    acc.Type = 'Customer';   
    
    insert acc;
    //Account acc = [select Account.Id from Account where Name='Kuliza'];    
    Opportunity opp = new Opportunity( Name = 'test opportunity',
                              AccountId = acc.Id,
                              StageName = 'Negotiations',
                              CloseDate = System.today(),
                              Type = 'New Business - Add',
                              Amount = 5558);
    insert opp;
    opp.StageName = '7. Closed/Won';
    update opp;
    
    AggregateResult[] results = [select MAX(cas_LastConnect__c) from Contact where Account.Id = :acc.Id and cas_LastConnect__c != null ];
    Map<String, Datetime> account_connectDate = new Map<String, Datetime>();
    for(AggregateResult ar : results){
        account_connectDate.put(String.valueof( ar.get('Id')), Datetime.valueof( ar.get('expr0')));
    }
        Datetime today = Date.today().addMonths(-6);
        Double casContribution = null;
        if(account_connectDate.containsKey(opp.AccountId)){
            Datetime lastConnectDate = account_connectDate.get(opp.AccountId);
            if(lastConnectDate > today){
                casContribution = opp.Amount;
            }
        }
    // Verification
    System.assertEquals(opp.ConnectAndSell_bookings_contribution__c, casContribution);
    Test.stopTest();
    }
}
 
Best Answer chosen by Ankit Singh 6
RatanRatan
User-added image

Make sure Always Run Ashynchronously checkbox checked

All Answers

RatanRatan
User-added image

Make sure Always Run Ashynchronously checkbox checked
This was selected as the best answer
Ankit Singh 6Ankit Singh 6
Thanks a lot..It worked.