+ Start a Discussion
Esti LeiserEsti Leiser 

apex test not running? (Says 0/0 test methods passed)

Hi! I wrote an apex test for a trigger I just wrote, and for some reason when I run the test it says "0/0 test methods passed".

I googled a bunch but couldn't find an answer, so I'm hoping you guys can help. I'm posting my trigger and class. The trigger acts when a lead is converted. What it does is populate a lookup field on the converted opportunity (contact__c) with the id of the converted contact.

Thanks in advance!

Trigger:
trigger AddPrimaryContactToConvertedOpp on Lead (After Update) {

// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE...

// [1] - Build map of converted Oppo Id to converted contactId
map <Id,Id> oIdToCIdMap = new map <Id,Id>();
for (Lead l : Trigger.new) 
    if (l.IsConverted && l.convertedOpportunityId != null)
        oIdToCIdMap.put(l.convertedOpportunityId,l.convertedContactId);

// [2] Update the converted Oppos
List<Opportunity> oUpdList = new List<Opportunity> ();
for (ID oId : oIdToCIdMap.keySet())
   oUpdList.add(new Opportunity(id = oId, contact__c = oIdToCIdMap.get(oId)));

update oUpdList;  // could be Database.update(oUpdList,false) if you want partial successes
}
Test class:
 
@isTest
public class TestAddPrimaryCToConvertedOppTrigger {

  static void testInsertLead(){
    Lead L1 = new Lead(LastName='last',FirstName='first',company='company');
    insert L1;

    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(L1.id);

    test.startTest();


    Database.LeadConvertResult lcr = Database.convertLead(lc);

      Contact C1 = [SELECT Id FROM Contact WHERE Id = :L1.ConvertedContactId];

      Opportunity O1 =  [SELECT Id FROM Opportunity WHERE Id = :L1.ConvertedOpportunityId];

    system.AssertEquals(O1.Contact__c,C1.ID);
    test.stopTest();
    }
}
BalajiRanganathanBalajiRanganathan
you test method does not have testmethod keyword or @isTest annotation

static void testInsertLead(){
change this line to 
 
@isTest
static void testInsertLead(){

 
Esti LeiserEsti Leiser
Thank you, that explains it. I see that you need @isTest before the class name, and also before the static void line.

Now the test ran, and failed :). The error message is:
System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, convertedStatus is required: [Status]
Any ideas? Thanks!!
BalajiRanganathanBalajiRanganathan
it looks like you have to set the status while creating test data.
 
Lead L1 = new Lead(LastName='last',FirstName='first',company='company', status='<leadStatus>');

udpate <leadStatus> to one value from status picklist on lead
Esti LeiserEsti Leiser
Hmm... That didn't fix it. I tried something else, and now I'm on to a new error (later in the test).

Updated apex:
@isTest
public class TestAddPrimaryCToConvertedOppTrigger {

    @isTest
  static void testInsertLead(){
    Lead L1 = new Lead(LastName='last',FirstName='first',company='company');
    insert L1;

    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(L1.id);
    LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);

    test.startTest();

    Database.LeadConvertResult lcr = Database.convertLead(lc);

    Contact C1 = [SELECT Id FROM Contact WHERE Id = :L1.ConvertedContactId];

    Opportunity O1 =  [SELECT Id FROM Opportunity WHERE Id = :L1.ConvertedOpportunityId];

    system.AssertEquals(O1.Contact__c,C1.ID);
    test.stopTest();
    }
}
The error is on line 18 (starts with CONTACT C1) - it says "System.QueryException: List has no rows for assignment to SObject"

Any ideas? Thanks!
 
BalajiRanganathanBalajiRanganathan
you have to use 
@isTest(SeeAllData=true) for your class 
@isTest(SeeAllData=true)
public class TestAddPrimaryCToConvertedOppTrigger {

 
Esti LeiserEsti Leiser
Thanks for answering! I made that change, and the test failed again, for the same reason.
 
BalajiRanganathanBalajiRanganathan

Looks like you dont have any picklist values is set as converted in Lead Object Status picklist field.

go to  Customize -> Lead -> Fields -> Status field.

choose any picklist value and make sure atleast one of them have converted checkbox is checked.
Esti LeiserEsti Leiser

Are you referring to the "Lead Status" field? The Closed - Converted picklist choice has the converted checkbox checked.
 

Also, the error is not on the line where it chooses the converted status, but on a later line -

Contact C1 = [SELECT Id FROM Contact WHERE Id = :L1.ConvertedContactId];
BalajiRanganathanBalajiRanganathan
use lcr.isSuccess() and  lcr.getErrors() method to check if there are any errors while converting.

System.debug('Success : ' +  lcr.isSuccess() );
for (Database.Error err : lcr.getErrors()) {
  System.debug('Error: ' +  err.getMessage() );
}
Esti LeiserEsti Leiser
Thanks! It seems to be giving the same error in the log:
15:17:02.413 (6413785447)|SOQL_EXECUTE_BEGIN|[21]|Aggregations:0|select Id from Contact where Id = :tmpVar1
15:17:02.420 (6420937180)|SOQL_EXECUTE_END|[21]|Rows:0
15:17:02.421 (6421078489)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

Class.TestAddPrimaryCToConvertedOppTrigger.testInsertLead: line 21, column 1
15:17:02.421 (6421092138)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

 
BalajiRanganathanBalajiRanganathan
did you check if the Database.convertLead(lc); is successful? to check that i asked you put the debugs as i gave in the previous reply.

the log you have given does not show any thing related to the system.debug i asked you to put.
 
Esti LeiserEsti Leiser
I put those debug lines in, but I guess I didn't paste that line. Here's the applicable line (I think) - looks like the convert worked.
15:17:02.413 (6413273806)|USER_DEBUG|[17]|DEBUG|Success : true

 
BalajiRanganathanBalajiRanganathan
I think you have to use methods from Database.LeadConvertResult to get the id of converted contact and opportunity.

So try using this
 
Contact C1 = [SELECT Id FROM Contact WHERE Id = :lcr.getContactId()];

    Opportunity O1 =  [SELECT Id FROM Opportunity WHERE Id = :lcr.getOpportunityId()];

 
Esti LeiserEsti Leiser
Okay so I tried changing the code to that, and when saving I get an error:
Method does not exist or incorrect signature: [Database.LeadConvert].getOpportunityId()
Any ideas?
 
BalajiRanganathanBalajiRanganathan
are you using lc or lcr?
you have to use lcr
Esti LeiserEsti Leiser
Aha, it was lc. I just switched it to lcr, and now I'm getting a new error:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Contact__c
Looks like the error is on this line: system.AssertEquals......

Now what?
 
BalajiRanganathanBalajiRanganathan
ok update the SOQL for Opportunity

Opportunity O1 = [SELECT Contact__c FROM Opportunity WHERE Id = :lcr.getOpportunityId()];
Esti LeiserEsti Leiser

Just made that change, and........

IT PASSED!!!

Thank you SO much for your help and your neverending patience!

Have a great day...