+ Start a Discussion
cml9cml9 

Need help in Unit Test for my Trigger 0% Coverage.

Please help me why its giving my test class is giving me 0% coverage.

Here is my Trigger Code.
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    Set<Id> contactId = new Set<Id>();
	id accountId;
    date myDate = date.valueOf(system.today());
    
    for (Direct_Opportunities__c d : Trigger.new){
        
	      System.debug(d.Contact__c);
          contactId.add(d.Contact__c);
      
       // Getting the Account Id from Direct Opportunity Contact__c
       // and assigned it to variable aId
       
        accountId = [select AccountId from Contact where id in :contactId].get(0).accountId;
		
        
	// Inserting of New Opportunity
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ' + d.Type__c;
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
        o.AccountId = accountId;
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;

        insert o;
        
        // Inserting of Tasks
        
        Task t = new Task();
        
        t.Subject = d.Type__c;
        t.ActivityDate = myDate + 1;
	    t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        t.WhoId = o.Contact__c;
        
        insert t;
        
        
    }
}

And here is my Test Class.
 
@istest

public class Unit_Test_DirectOpp {

    static testMethod void TestonDirectOpp() {
   		
     test.startTest();

        Account acct = new Account(Name = 'Test Account ');

        insert acct;
     
        Contact cont = new Contact(LastName = 'Last', AccountId = acct.Id );

        insert cont;
    
    	Direct_Opportunities__c d = new Direct_Opportunities__c(Assigned_BC__c = cont.OwnerId, Contact__c = cont.Id, Type__c = 'Type', Campaign__c = 'MyCampaign');


        Opportunity Opp = new Opportunity(AccountId = acct.Id,  Contact__c = cont.Id, Name = 'Test Opportunity', StageName ='New' , CloseDate = date.today());

        insert Opp;

      
        Task t = new Task(Subject='Subject', ActivityDate = date.today(), Status = 'Not Started', Priority = 'High', OwnerId = Opp.OwnerId, WhatId = Opp.Id, WhoId = cont.id);
        insert t;

    
        
        test.stopTest();
       
 }
}

 
Veenesh VikramVeenesh Vikram
Hi,

You didnt insert "Direct_Opportunities__c".
If you will insert it, your coverage will be there.

Veenesh
cml9cml9
yes, I found that out but its still the same. It has some kind of an error with DML manipulation.
Veenesh VikramVeenesh Vikram
Can you provide me more details about what error you are getting.