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
Rahul Chauhan 33Rahul Chauhan 33 

My requirement is Write a test class ....coverage 80% above

My apex code :-
public class Acctcontroller
{
 // Delaration   
 public Account accobj{get;set;}
 public Opportunity oppobj{get;set;}
 public List<Product2> Productlist{get;set;}
 public List<wrapperclass> wraplist{get;set;}
 Public List<Pricebook2>   pbookobj{get;set;}
 Public Set<Id>      productidset{get;set;}
 public string oppid;
 public List<Opportunity> pbookidlst{get;set;}
 Public List<Pricebookentry> pbe{get;set;}   
 Public Map<Id,Double> mapunitprice{get;set;}
 Public Map<Id,string> mapproduct{get;set;}
 
 
 // constructor
 public Acctcontroller() {
     wraplist = new List < wrapperclass > ();
     oppobj = new Opportunity();
     pbookobj = new List < Pricebook2 > ();
    
     String accId = ApexPages.currentPage().getParameters().get('Id');
     System.debug('Accountid' + accId);
     oppid = ApexPages.currentPage().getParameters().get('oppId');

     if (accId != null && accId.trim() != '') {
         accobj = [SELECT Id, 
                             Name 
                       FROM Account 
                          WHERE Id = : accId];
         System.debug('hey' + accobj);
         oppobj.AccountId = accId;
     }

     Productlist = [SELECT Id, 
                    Name, Productcode 
                    FROM Product2];
     System.debug('Plistvalue' + Productlist);

     for (product2 prodt: Productlist) {
         wrapperclass wrap = new wrapperclass();
         wrap.pro = prodt;
         wraplist.add(wrap);
     }

 }

 public Pagereference save() {
     list<pricebook2> istpbook = [SELECT Id,
                                           Name 
                                      FROM pricebook2
                                      LIMIT 1];
     if(!istpbook.isEmpty())
     oppobj.pricebook2id = istpbook[0].id;
     insert oppobj;
     Pagereference objPG = new Pagereference('/apex/opportunityproductline?oppid=' + oppobj.id);
     objPG.setRedirect(true);
     return objPG;

 }

 public Pagereference saveproduct() {
     productidset = new set <Id>();
     for (wrapperclass wss: wraplist) {
         if (wss.checkbx == true) {

             productidset.add(wss.pro.id);
         }
         }
        System.debug('productsetid'+productidset); 
     
     
    pbookidlst = [SELECT Id,
                           Name,
                           Pricebook2id 
                      FROM Opportunity 
                      WHERE Id = :oppId];
    System.debug('productset1'+pbookidlst);
   
     pbe =[SELECT id,
          UnitPrice,
          Product2Id 
          FROM pricebookentry 
          WHERE product2.id in :productidset];
     
     System.debug('pricebookunitprice'+pbe);
 
     mapunitprice = new Map<id,Double>();
     for(pricebookentry p : pbe)
     {
      
      mapunitprice.put(p.Product2Id,p.UnitPrice);
      
     } 
      System.debug('Mapid'+mapunitprice);
      
 
     list < OpportunityLineItem > lstOLI = new list < OpportunityLineItem > ();
     for (wrapperclass ws: wraplist) {
         if (ws.checkbx == true) {
             OpportunityLineItem objOLI = new OpportunityLineItem();
             objOLI.Product2Id = ws.pro.id;
             objOLI.OpportunityId = oppid;
             objOLI.UnitPrice=mapunitprice.get(ws.pro.id);
             
             System.debug('prod id'+ws.pro.id+'\n--map-'+mapunitprice.get(ws.pro.id));
             System.debug('id11'+ws.pro.id);
             
             objOLI.Quantity = ws.qty;
             System.debug('Quantity'+objOLI.Quantity);
             System.debug('Tp'+objOLI.totalprice);
             // objOLI.totalprice = 100; 
             //objOLI.UnitPrice=map.get(ws.pro.id);
             lstOLI.add(objOLI);
             
         }

     }
     insert lstOLI;
     System.debug('waplistvalue' + lstOLI);
     Pagereference objPGg = new Pagereference('https://ap.salesforce.com?id=accid');
     objPGg.setRedirect(true);
     return objPGg;
 }

 public class wrapperclass {
     public product2 pro {get;set;}
     public Boolean checkbx {get;set;}
     public integer qty {get;set;}
     
     // Wrapper Constructor
     public wrapperclass() {
         pro = new product2();
         checkbx = false;
        
     }
 }
 }

I am trying this :- 
@isTest
public class Acctcontroller_Test {
       
     static testMethod void Updatefield() {
    Account acc = new Account();
        acc.Name = 'Test';
        acc.Rating = 'Hot';
        acc.Status__c= 'Active';
        insert acc;
        
        Opportunity opp = new Opportunity();
        opp.name = 'Test';
        opp.AccountId = acc.Id;
        opp.Amount =1500 ; 
        opp.StageName = 'Closed Won';
        opp.CloseDate = Date.today();
        opp.Type = 'New Customers';
        insert opp;
        
        
        
        Test.StartTest(); 
        Acctcontroller refClass = new Acctcontroller ();
        Test.stopTest();
    }       
}


but its coverage only 20% 
Please help me.........
Thanx in adv.*
Best Answer chosen by Rahul Chauhan 33
Raj VakatiRaj Vakati
Try this .. its 78 %
 
@isTest
public class Acctcontroller_Test {
    
    static testMethod void Updatefield() {
        Account acc = new Account();
        acc.Name = 'Test';
        acc.Rating = 'Hot';
        // acc.Status__c= 'Active';
        insert acc;
        
        Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity();
        opp.name = 'Test';
        opp.AccountId = acc.Id;
        opp.Amount =1500 ; 
        opp.StageName = 'Closed Won';
        opp.CloseDate = Date.today();
        opp.Type = 'New Customers';
        opp.PriceBook2 = customPB;
        
        insert opp;
        
        
        
        Opportunity opp1 = new Opportunity();
        opp1.name = 'Test';
        opp1.AccountId = acc.Id;
        opp1.Amount =1500 ; 
        opp1.StageName = 'Closed Won';
        opp1.CloseDate = Date.today();
        opp1.Type = 'New Customers';
        opp1.PriceBook2 = customPB;
        Test.StartTest(); 
        Acctcontroller refClass = new Acctcontroller ();
        refClass.oppobj = opp1;
        refClass.accobj = acc ;
        refClass.save() ; 
        refClass.saveproduct() ;
        Test.stopTest();
    }       
}

 

All Answers

Chris EagerChris Eager
Hi Rahul,

The reason you are only getting 20% is because you are not calling your other methods in your test class. You need to call both the save() and saveproduct() methods to get the rest of your coverrage.

Also, please considering using a test setup (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm) to create your base line test data instead of in your test method.  
Raj VakatiRaj Vakati
Try this .. its 78 %
 
@isTest
public class Acctcontroller_Test {
    
    static testMethod void Updatefield() {
        Account acc = new Account();
        acc.Name = 'Test';
        acc.Rating = 'Hot';
        // acc.Status__c= 'Active';
        insert acc;
        
        Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity();
        opp.name = 'Test';
        opp.AccountId = acc.Id;
        opp.Amount =1500 ; 
        opp.StageName = 'Closed Won';
        opp.CloseDate = Date.today();
        opp.Type = 'New Customers';
        opp.PriceBook2 = customPB;
        
        insert opp;
        
        
        
        Opportunity opp1 = new Opportunity();
        opp1.name = 'Test';
        opp1.AccountId = acc.Id;
        opp1.Amount =1500 ; 
        opp1.StageName = 'Closed Won';
        opp1.CloseDate = Date.today();
        opp1.Type = 'New Customers';
        opp1.PriceBook2 = customPB;
        Test.StartTest(); 
        Acctcontroller refClass = new Acctcontroller ();
        refClass.oppobj = opp1;
        refClass.accobj = acc ;
        refClass.save() ; 
        refClass.saveproduct() ;
        Test.stopTest();
    }       
}

 
This was selected as the best answer