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
lightdreamlightdream 

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.

=====================Myclass=================================

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;   }
}
@isTest
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;
       if(acc!=null)
       {
         acc=new account(name='balaji');
         insert acc;
       
       }
        if(ct!=null)
         {
           ct=new contact();
           ct.accountid=acc.id;
           insert ct;
         }
         if(opp!=null)
         {
         
           opp=new opportunity();
         }
      /*  insert acc;
        ct.lastname='balaji';
        ct.accountid=acc.id;
        insert ct;
        opportunity opp=new opportunity(name='balaji',stagename='prospecting',forecastcategoryname='pipeline',closedate=system.today());
        opp.accountid=acc.id;
        try{
        if(opp!=null) 
        opportunity opp1=new opportunity();       
        insert opp;
        }
        catch(DMLException e1)
        {
          system.debug(e1.getMessage());
          }
       OpportunityContactRole ro=new OpportunityContactRole();
        ro.opportunityid=opp.id;
        ro.contactid=ct.id;
        try{
               if(ro!=null)
                insert ro;
        }
        catch(DMLException e)
        {
          system.debug(e.getMessage());
        
        }*/
       s.getAccount();                        // Call Function of Original class
       s.getContact();
       s.getOpportunity();
       s.getrole();
         s.step1();
         s.step2();
         s.step3();
         s.cancel();
         s.save();   
        
    }    
    
}

 

NK123NK123

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.

 

~NK 

ngabraningabrani

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.