-1
    public with sharing class LeadEventCreation {

    public PageReference doSave() {
        insert objL;
        objL.recordTypeId = apexpages.currentPage().getparameters().get('id');// these lines are not covering showing in red colour
        update objL;                                                          // these lines are not covering showing in red colour

        objE.whoId = objL.id;                                                 // these lines are not covering showing in red colour
        insert objE;                                                          // these lines are not covering showing in red colour
        return (new pagereference('/'+objL.id).setredirect(true));            // these lines are not covering showing in red colour
    }

    public Lead objL{get;set;}
    public Event objE{get;set;}
    public LeadEventCreation(){
        objL = new Lead();
        objE = new Event();
    }
}
    @isTest
    private static void mytestclass()
    {
        test.startTest();
            LeadEventCreation objLEC= new LeadEventCreation ();
                try{
                objLEC.doSave();
                }
                catch(exception e){}

                lead obj1= new lead();
                obj1.LastName= 'test name';
                try{
                insert obj1;
                }
                catch(exception e){}

                lead obj2= new lead();
                obj2.LastName= 'test name1';
                try{
                update obj2;
                }
                catch(exception e){}
                ApexPages.currentPage().getParameters().put('id','nytest');
        test.stopTest();
    }
}

1 Answer 1

2

It looks likely that the line:

        insert objL;

Is causing an exception and since you have:

        try{
        objLEC.doSave();
        }
        catch(exception e){}

You are swallowing the exception and not seeing what the issue is and the test is not going any further for doSave().

You would need to prevent the exception to allow doSave() to finish, i.e. ensure that the Lead is inserted correctly/without failure.

4
  • Thank Phil for Quick replay Can you suggest me how to do that please
    – rakesh
    Commented Nov 28, 2012 at 12:26
  • @rakesh, remove the Try/Catch around objLEC.doSave() and you will soon see what the issue is that is stopping you insert the record. Commented Nov 28, 2012 at 12:27
  • Im getting the test failure message if i remove try & catch System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must review the marketing preferences section before you can save this record: []
    – rakesh
    Commented Nov 28, 2012 at 12:32
  • 2
    @rakesh, ok - you asked why the lines weren't being tested and this is the answer. Line 1 of your doSave() fails which means the subsequent lines aren't tested (and show up in red). The insert is failing because of a validation error which according to what you say means "You must review the marketing preferences section before you can save this record". You are are missing some information on the Lead object you are trying to insert, i.e. its an empty record looking at it. Commented Nov 28, 2012 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .