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
Javier MaldonadoJavier Maldonado 

Test Code - No errors but not code coverage

Hello everyone,

Can somebody give me a hand... I just createD this test code, but it is not giving me any code coverage, and no errors neither...

TRIGGER
trigger SelectRecordType on Case (before insert) { 
    for (case cs :Trigger.new){
        if(cs.Type == 'Billing Adjustment'){
            cs.RecordTypeId='012180000004KrD'; 
            }
        else if(cs.Type == 'RMA'){
                cs.RecordTypeId='01240000000IbO4';
            }
    }  
}

TEST
@isTest(seeAllData=True)
public class SelectRecordType_Test {
    static TestMethod void SelectRecordType(){
        case c = new case();
        string tp = c.Type;
        if(tp == 'Billing Adjustment'){
            c.RecordTypeId='012180000004KrD'; 
        } else if(tp == 'RMA'){
            c.RecordTypeId='01240000000IbO4';
        }
    }
}
Best Answer chosen by Javier Maldonado
ManojjenaManojjena
Hi Javier ,
Try with below code it will help ! 
@isTest
public class SelectRecordType_Test {
   private  static TestMethod void unitTest(){
        case cs = new case();
        cs.Type='Billing Adjustment';
		cs.Status='New';
		cs.Origin='Web';
		Insert cs;
		cs=[SELECT id,Type FROM Case WHERE id=:cs.Id];
		System.assertEquals(cs.Type,'Billing Adjustment');
    }
	private static TestMethod void unitTest1(){
        case cs = new case();
        cs.Type='RMA';
		cs.Status='New';
		cs.Origin='Web';
		Insert cs;
		cs=[SELECT id,Type FROM Case WHERE id=:cs.Id];
		System.assertEquals(cs.Type,'RMA');
    }
}

Thanks 
Manoj

All Answers

kaustav goswamikaustav goswami
The trigger runs only if a case record is inserted. Fro myour test class you have not inserted any case records. Also you do not need to set seeAllData to true.

Thanks,
Kaustav
Andy BoettcherAndy Boettcher
A few things:

1 - Never never never never use "seeAllData=true" unless you ABSOLUTELY have to (there are some instances where you need to do it, but this isn't one of them).

2- Your unit test is designed to run the targeted (trigger) code, so you have to do a DML insert in your test class to invoke it.  You will end up creating two individual cases (each with the RecordType in question) and insert both.

Another note - you also want to avoid specific record/recordtype Ids in your code (regular and unit test) - you can query the RecordType object on the Name/DeveloperName to get the id back.  If you leave the Id in the code - you run the risk of this not working when you promote the code to Production.
kaustav goswamikaustav goswami
Adding a code snippet to give you an idea about what I and Andy are referring to.

Sample Trigger
trigger SelectRecordType on Case (before insert){
	// query the record types object
	List<RecordType> recList = new List<RecordType>();
	recList = [SELECT Id, Name FROM RecordType WHERE SObjectType = 'Case' AND --Your other fileters in required];
	String billingREcTypeId = '';
	String rmaRecTypeId = '';
	if(recList != null && recList.size() > 0){
		for(RecordType rec : recList){
			if(rec.Name == 'Billing....your actual value'){
				billingREcTypeId = rec.Id;
			}else{ // considering there are only two record types returned by the above query
				rmaRecTypeId = rec.Id;
			}
		}
	}
	for(Case cs : Trigger.new){
		if(cs.Type == 'Billing Adjustment'){
			cs.RecordTypeId = billingREcTypeId;
		}else if(cs.Type == 'RMA'){
			cs.RecordTypeId = rmaRecTypeId;
		}
	}
}

Test Class
@isTest
public class SelectRecType_Test{
	static testmethod void testScenarioOne(){
		List<Case> caseList = new List<Case>();
		Case c1 = new Case();
		c1.Type = 'Billing Adjustment';
		c1.xxx = '...other required fields to create the case needs to be set';
		caseList.add(c1);
		Case c2 = new Case();
		c2.Type = 'RMA';
		c1.xxx = '...other required fields to create the case needs to be set';
		caseList.add(c2);
		insert caseList;
		// add code to assert the outcome from your trigger
	}
}

Thanks,
Kaustav
ManojjenaManojjena
Hi Javier ,
Try with below code it will help ! 
@isTest
public class SelectRecordType_Test {
   private  static TestMethod void unitTest(){
        case cs = new case();
        cs.Type='Billing Adjustment';
		cs.Status='New';
		cs.Origin='Web';
		Insert cs;
		cs=[SELECT id,Type FROM Case WHERE id=:cs.Id];
		System.assertEquals(cs.Type,'Billing Adjustment');
    }
	private static TestMethod void unitTest1(){
        case cs = new case();
        cs.Type='RMA';
		cs.Status='New';
		cs.Origin='Web';
		Insert cs;
		cs=[SELECT id,Type FROM Case WHERE id=:cs.Id];
		System.assertEquals(cs.Type,'RMA');
    }
}

Thanks 
Manoj
This was selected as the best answer
Javier MaldonadoJavier Maldonado
Thanks Manoj...