+ Start a Discussion

Test Class not covered the code coverage

Hi all,

I written one test class for Trigger but 0% Code covered.

trigger OpportunityStageUpdate on Opportunity (after update) {
    List < ID > oid = New List < ID > ();
    for (Opportunity o: Trigger.new) {
        if (o.StageName == 'Closed Lost' && Trigger.oldMap.get(o.Id).StageName != 'Closed Lost') {
if(oid.size() > 0){
    List < ProfessionalServicesRequest__c> PSRUpdateList= [SELECT Id,Status__c from ProfessionalServicesRequest__c WHERE Opportunity__c in : oid];
    for (ProfessionalServicesRequest__c objPSR: PSRUpdateList) {
        if(objPSR.Status__c =='Request Complete' || objPSR.Status__c =='Request Cancelled'){
         } else {
             objPSR.Status__c = 'Request Cancelled';
    }   update PSRUpdateList;

Test Class:-

private class OpportunityStageUpdateTestclass {
@isTest static void myTest() {
// OpportunityStageUpdate os = new OpportunityStageUpdate();
        Account a = new Account(Name='Test',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
        insert a;
        Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;
        Opportunity O = new Opportunity(Name='TestPipelineChange',AccountId=a.id,End_User__c=a.id,   Contact__c=c.id,CloseDate=System.today(),Type='New                            Customer',Order_Type__c='New',  Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
        insert O;
       ProfessionalServicesRequest__c psr = new ProfessionalServicesRequest__c(Request_Type__c='TestRequest',
           Complexity__c='TestComplex',Completion_By__c=System.Today(),Assigned_To__c='TestAssigned',Status__c='Request Submitted',
           insert psr;
           psr.Status__c='Request Cancelled';
          update psr;       

Thanks in Adv.
Best Answer chosen by kumar.fdc81.3902978579608325E12
You won't cover any of the trigger as it is an after update trigger on the opportunity sobject, but you aren't updating your opportunity, just inserting it.

I reckon you just want to change the stagename of O to ClosedLost and then update it, but that's based on a cursory glance at your code without knowing our use case.