+ Start a Discussion
sridhar sridharsridhar sridhar 

Code Coverage not success

//These two sections are not covering in code coverage  plz provide a solution
section1
for(Case selectedCase : selectedCases)
        {
           caselist.add(selectedCase);
section 2
catch (DMLException e){       
         for(case c :caselist){
         c.adderror('xxxxxx.');
sridhar sridharsridhar sridhar
The above code is not coming under code coverage .. because of this i am unable to deploy in PROD.. please provide solution
Sanka VimukthiSanka Vimukthi
Hi Sridhar,

Section 1,

Try to insert some cases before you are trying to call the particular method from your test method. 

Section 2,

Intetionally insert some data which can cause an exception in your code when you are executing. I'd recommend that you do that in a seperate test method.
sridhar sridharsridhar sridhar
can you please post some sample code
Sanka VimukthiSanka Vimukthi
Class :
 
public class MyClass 
{
    public list<Case> SelectedCases{get;set;}
    
    public void MyMethod()
    {
        SelectedCases = [SELECT Id,CaseNumber FROM Case];
        
        for(Case c : SelectedCases)
        {
            //Do Something
		}
	}
    
    public void MyMethod2()
    {
        try
        {
            Case c1 = [SELECT Id,CaseNumber FROM Case LIMIT 1];
            
            String numberString = c1.CaseNumber;
        }
        catch(Exception e)
        {
            System.debug('Error ::: ' + e.getMessage());
		}
	}
}

Test Class :
 
@isTest
public class MYTestClass 
{
	public static testMethod void testMethod1()
    {
        Case c = new Case();
        insert c;
        
        MyClass m = new MyClass();
        
        Test.startTest();
        	m.MyMethod();
        	m.MyMethod2();
        Test.stopTest();
        
	}
    
    public static testMethod void testMethod2()
    {
        MyClass m = new MyClass();
        
        Test.startTest();
        	m.MyMethod2();
        Test.stopTest();       
	}
}

See, I didnt insert a case in the second method and when the code is getting executed it will throw an exception.

Hope this helps.
Amit Chaudhary 8Amit Chaudhary 8
Please post your full code so that we can help you