-1
@isTest
public class ConvertTestClass {

    public static testMethod void test1() {

        Lead__c ld = new Lead__c ();
        ld.First_Name__c = 'Test';
        ld.Name    ='first';
        ld.Company__c ='Test company';
        ld.Status__c = 'Qualified';
        insert ld;

        Test.StartTest(); 

        PageReference pageRef = Page.RedirectingToLead; 
        Test.setCurrentPage(pageRef);


        ApexPages.StandardController sc = new ApexPages.StandardController(ld);
        ConvertTheLead testAccPlan = new ConvertTheLead(sc);
        testAccPlan.RedirecttoSite();
        testAccPlan.convertbutton();
        Test.StopTest();

    }

    public static testMethod void test2() {

        Lead__c ld = new Lead__c ();
        ld.First_Name__c = 'Test';
        ld.Name    ='first';
        ld.Company__c ='Test company';
        // Set Status as any valid value other then "Qualified"
        ld.Status__c = 'New'; 
        insert ld;

        Test.StartTest(); 

        PageReference pageRef = Page.RedirectingToLead; 
        Test.setCurrentPage(pageRef);
        PageReference RedirectingToLead = extension.myUpdate();


        ApexPages.StandardController sc = new ApexPages.StandardController(ld);
        ConvertTheLead testAccPlan = new ConvertTheLead(sc);
        testAccPlan.RedirecttoSite();
        testAccPlan.convertbutton();
        Test.StopTest();
        system.assert(ApexPages.addMessage(), 'There should be an error');
    system.assertEquals(null, RedirectingToLead, 'Redirect should be cancelled');

    }

}
  • What is your question? What do you want to achieve? Please explain.. – Aditya Vijay Apr 13 at 6:41
  • I want to make my code coverage which is 91% to 100% its not taking the exception part I am getting errors – smn Apr 13 at 6:48
  • you need to write the erroneous code in test class which will generate the exception, you need to add the main class code/snippet in order to get the help! – Aditya Vijay Apr 13 at 6:51
  • I added and I ma getting error in line 4 method does not exits 27 syntax error – smn Apr 13 at 7:01
  • I meant to add here in the question, so everybody can look at it and suggest. – Aditya Vijay Apr 13 at 7:15
0
ApexPages.StandardController sc = new ApexPages.StandardController(ld);
ConvertTheLead testAccPlan = new ConvertTheLead(sc);
testAccPlan.RedirecttoSite();
testAccPlan.convertbutton();
Test.StopTest();    
List<Apexpages.Message> errorMsgs = ApexPages.getMessages();
Boolean errorFound = false;
for(Apexpages.Message msg:errorMsgs){
    if (msg.getDetail().contains(<expected error message>){
      errorFound = TRUE;
    }
}
System.assertEquals(errorFound, true);

Use above code in you second test method to verify VF page error message. Modify code where ever required.

Look into Controller method and note what causes the error message being shown on the page, prepare test record data accordingly that same error message will be added by the controller method when called from the test method.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.