+ Start a Discussion

Test Class

Hi all,

I have a class and test class for it .But it covers  68% of  the code of my class. so please help me to cover 100% of my code.


public class newOpportunityController {
   // These four member variables maintain the state of the wizard.         // When users enter data into the wizard, their input is stored         // in these variables.          Account account;   Contact contact;   Opportunity opportunity;   OpportunityContactRole role;

   // The next four methods return one of each of the four member         // variables. If this is the first time the method is called,         // it creates an empty record for the variable.         public Account getAccount() {      if(account == null) account = new Account();      return account;   }
   public Contact getContact() {      if(contact == null) contact = new Contact();      return contact;   }
   public Opportunity getOpportunity() {      if(opportunity == null) opportunity = new Opportunity();      return opportunity;   }
   public OpportunityContactRole getRole() {      if(role == null) role = new OpportunityContactRole();      return role;   }

   // The next three methods control navigation through         // the wizard. Each returns a PageReference for one of the three pages         // in the wizard. Note that the redirect attribute does not need to         // be set on the PageReference because the URL does not need to change         // when users move from page to page.         public PageReference step1() {      return Page.opptyStep1;   }
   public PageReference step2() {      return Page.opptyStep2;   }
   public PageReference step3() {      return Page.opptyStep3;   }

   // This method cancels the wizard, and returns the user to the          // Opportunities tab          public PageReference cancel() {    try{             PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();            system.debug('--------------ade--');            opportunityPage.setRedirect(true);            return opportunityPage;             }            catch(system.DMLException e){            system.debug('---------kih-------');            ApexPages.addmessages(e);            return null;            }    }
   // This method performs the final save for all four objects, and         // then navigates the user to the detail page for the new         // opportunity.         public PageReference save() {
      // Create the account. Before inserting, copy the contact's            // phone number into the account phone number field.            account.phone = contact.phone;      insert account;
      // Create the contact. Before inserting, use the id field            // that's created once the account is inserted to create            // the relationship between the contact and the account.            contact.accountId = account.id;      insert contact;
      // Create the opportunity. Before inserting, create             // another relationship with the account.            opportunity.accountId = account.id;      insert opportunity;
      // Create the junction contact role between the opportunity            // and the contact.            role.opportunityId = opportunity.id;      role.contactId = contact.id;      insert role;
      // Finally, send the user to the detail page for             // the new opportunity.      

      PageReference opptyPage = new ApexPages.StandardController(opportunity).view();      opptyPage.setRedirect(true);
      return opptyPage;   }
private class newOpportunityController_TestMethod // Name of the test class, Can be any name
    static testmethod void newOpportunityController_TestMethod()   // Name of the test method. Must be the same as the class name 
        newOpportunityController s = new newOpportunityController();   // Initialize variable for original class 
        contact ct;
        account acc;
        opportunity opp;
        opportunitycontactrole opp1;
         acc=new account(name='balaji');
         insert acc;
           ct=new contact();
           insert ct;
           opp=new opportunity();
      /*  insert acc;
        insert ct;
        opportunity opp=new opportunity(name='balaji',stagename='prospecting',forecastcategoryname='pipeline',closedate=system.today());
        opportunity opp1=new opportunity();       
        insert opp;
        catch(DMLException e1)
       OpportunityContactRole ro=new OpportunityContactRole();
                insert ro;
        catch(DMLException e)
       s.getAccount();                        // Call Function of Original class



When you run this test case, system allows you to see what percent of code is covered. In your case its 68%, so just click on the "newOpportunityController" file and system will be showing which lines of code is not covered under the test case. You can update your test case to cover those lines.


In my experience, the remainder of the code which is not covered usually falls under the "If" block of code, so try looking into your code and update test case to cover all the remainder of code lines.




Looking at the testcase, all statements except the ones under the catch block should get executed

catch(system.DMLException e){            

You need to click on the link that says 68%. That will indicate which lines of Apex controller are not being executed in the test case.