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
shakila Gshakila G 

My Apex class unable to cover by Test class?

Hi All,

Am wrote a test class for my Apex class. After that also my class not covered by 0%.

Kindly let me know where am doing the mistake?

Hi,

Kindly anyone suggests me to write a test class for this Apex class?

public class IssuedConsumable{

Dispatched_Raw_Material__c DPRM= new Dispatched_Raw_Material__c();

public list<Dispatched_Raw_Material__c> listAccount{get;set;}

{
listAccount=new list<Dispatched_Raw_Material__c>();
listAccount.add(DPRM);
}

Public void addRows()
{
Dispatched_Raw_Material__c acc = new Dispatched_Raw_Material__c();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Created Successfully'));    

}
Return page.IssuedConsumable;

}
}

My Test Class:

@isTest(seealldata=true)
Public class IssuedConsumable_test{
    static testMethod void IssuedConsumable_test(){
        
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        insert lstprod; 
        
        Integer indexvalue = 0;
        if(lstprod.size()>0)
        {
            for(pricebookentry irow :lststdPbe)
            {
                irow.product2id = lstprod[indexvalue].id;
                
                indexvalue++;
            }
        }
        
        
        insert lststdPbe;
        
        insert lstPB; 
        
        
        if(lstprod.size()>0)
        {
            for(pricebook2 irow :lstPB)
            {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);
                }
            }
        }

        insert lstPbentry;
        insert acc[0];
        
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        update Opp[0];
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
            pricebookentryid=lstPbentry[0].id);
        insert OL; 
        
     
        
        Tax_Master__c   txm= new Tax_Master__c(CGST__c=14.00,HSN_Code__c=lstprod[0].HSN_Code__c,IGST__c=28.00,SGST__c=14.00, Start_Date__c =  Date.newInstance(2017, 07, 06), End_Date__c = System.today()+2);
        insert txm;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 00000000;
        Q.Quote_Number__c ='000000';
        Q.Approved__c = false;
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = lstPB[0].id;
        Q.Tax_Classification__c='IGST';
        Q.Discount_Amount__c = 100;
       
        
        insert Q;  
       
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id,SGST__c=6, CGST__c=6,IGST__c =0,Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL;  
        
            
        
    
    
    
    
        Project__c p = new Project__c(Name = 'NewProject',Project_Type__c = 'New PE',
        Opportunity__c = Opp[0].id,  
                       Quote__c = q.id);
        insert p;
        
        Product2 prod = new Product2(Name = 'ProdTest', CST__c = 2, Central_Excise__c = 3, VAT__c = 4, 
                        Family = '');
        insert prod; 
        
        Employee__c e = new Employee__c(Name = 'lakshmanan');
        insert e; 
               
        Project_Item__c pi = new Project_Item__c(Project__c = p.id,Status__c = 'In Progress',Product__c = prod.id,
                                 Technician_Member__c = 'suren; lakshmanan');
        insert pi;  

        
        QuoteLineItem qli = new QuoteLineItem (QuoteId = q.id,PricebookEntryId = lstPbentry[0].id, Quantity = 10,
                            UnitPrice = 100);
        insert qli;                    

        
         RawMaterial__c rm1 = new RawMaterial__c(Name = 'Painting Mask',Unit__c='MM');
        insert rm1;
       
        
       Raw_Material_Inventory__c  RW= New Raw_Material_Inventory__c(Name='Painting Mask');
       
        insert  RW ;  
        
        Dispatched_Raw_Material__c DR= new Dispatched_Raw_Material__c(Raw_Material_Inventory__c=RW.ID);
        insert DR;      
         
         IssuedConsumable RMR = New IssuedConsumable();
         rmr.addRows();
         rmr.saveAccount();
          
    }
}
 
shakila Gshakila G
Hi,
Kindly give a suggestion. I want to complete this in High priority.
 
Raj VakatiRaj Vakati
Your test class is looking good .. Can you check is your test is the success or failing .. i think it's failing .. Get the logs and share with me to find the issue 
shakila Gshakila G
Hi Raj,

I have entered all the mandatory information even though am getting the below error.

User-added image
Raj VakatiRaj Vakati
Looks like Raw_Material_Inventory__c  ovject is missing required fields.. Can you insert the complete required field on this object .. 

Go to salesforce tab and see what all are required and insert those into Raw_Material_Inventory__c   object in test class


 Raw_Material_Inventory__c  RW= New Raw_Material_Inventory__c(Name='Painting Mask');
// Add all required fileds and run the test class       
        insert  RW ;  

 
shakila Gshakila G
If I mention all the fields in Ram Material Inventory am getting a duplicate Value error like below

User-added image
Raj VakatiRaj Vakati
Make Sure name is all the values are uniquer like name and all 

If you can share the code for Raw_Material_Inventory__c   i will rewrite it for you
shakila Gshakila G
Test Class;

@isTest(seealldata=true)
Public class IssuedConsumable_test{
    static testMethod void IssuedConsumable_test(){
        
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        insert lstprod; 
        
        Integer indexvalue = 0;
        if(lstprod.size()>0)
        {
            for(pricebookentry irow :lststdPbe)
            {
                irow.product2id = lstprod[indexvalue].id;
                
                indexvalue++;
            }
        }
        
        
        insert lststdPbe;
        
        insert lstPB; 
        
        
        if(lstprod.size()>0)
        {
            for(pricebook2 irow :lstPB)
            {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);
                }
            }
        }

        insert lstPbentry;
        insert acc[0];
        
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        update Opp[0];
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
            pricebookentryid=lstPbentry[0].id);
        insert OL; 
        
     
        
        Tax_Master__c   txm= new Tax_Master__c(CGST__c=14.00,HSN_Code__c=lstprod[0].HSN_Code__c,IGST__c=28.00,SGST__c=14.00, Start_Date__c =  Date.newInstance(2017, 07, 06), End_Date__c = System.today()+2);
        insert txm;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 00000000;
        Q.Quote_Number__c ='000000';
        Q.Approved__c = false;
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = lstPB[0].id;
        Q.Tax_Classification__c='IGST';
        Q.Discount_Amount__c = 100;
       
        
        insert Q;  
       
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id,SGST__c=6, CGST__c=6,IGST__c =0,Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL;  
        
            
        
    
    
    
    
        Project__c p = new Project__c(Name = 'NewProject',Project_Type__c = 'New PE',
        Opportunity__c = Opp[0].id,  
                       Quote__c = q.id);
        insert p;
        
        Product2 prod = new Product2(Name = 'ProdTest', CST__c = 2, Central_Excise__c = 3, VAT__c = 4, 
                        Family = '');
        insert prod; 
        
        Employee__c e = new Employee__c(Name = 'lakshmanan');
        insert e; 
               
        Project_Item__c pi = new Project_Item__c(Project__c = p.id,Status__c = 'In Progress',Product__c = prod.id,
                                 Technician_Member__c = 'suren; lakshmanan');
        insert pi;  

        
        QuoteLineItem qli = new QuoteLineItem (QuoteId = q.id,PricebookEntryId = lstPbentry[0].id, Quantity = 10,
                            UnitPrice = 100);
        insert qli;                    

        
         RawMaterial__c rm1 = new RawMaterial__c(Name = 'Painting Mask',Unit__c='MM');
        insert rm1;
       
        
       Raw_Material_Inventory__c  RW= New Raw_Material_Inventory__c(Name='Painting Mask',
       RawMaterial__c=rm1.ID,Min_Quantity_Check__c=True,Minimum_Stock_Quantity__c=100);
       
        insert  RW ;  
        
        Dispatched_Raw_Material__c DR= new Dispatched_Raw_Material__c(
        Raw_Material_Inventory__c=RW.ID);
        insert DR;      
         
         IssuedConsumable RMR = New IssuedConsumable();
         rmr.addRows();
         rmr.saveAccount();
          
    }
}
Raj VakatiRaj Vakati
Use this code
 
@isTest(seealldata=false)
Public class IssuedConsumable_test{
    static testMethod void IssuedConsumable_test(){
        
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        insert lstprod; 
        
        Integer indexvalue = 0;
        if(lstprod.size()>0)
        {
            for(pricebookentry irow :lststdPbe)
            {
                irow.product2id = lstprod[indexvalue].id;
                
                indexvalue++;
            }
        }
        
        
        insert lststdPbe;
        
        insert lstPB; 
        
        
        if(lstprod.size()>0)
        {
            for(pricebook2 irow :lstPB)
            {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);
                }
            }
        }

        insert lstPbentry;
        insert acc[0];
        
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        update Opp[0];
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
            pricebookentryid=lstPbentry[0].id);
        insert OL; 
        
     
        
        Tax_Master__c   txm= new Tax_Master__c(CGST__c=14.00,HSN_Code__c=lstprod[0].HSN_Code__c,IGST__c=28.00,SGST__c=14.00, Start_Date__c =  Date.newInstance(2017, 07, 06), End_Date__c = System.today()+2);
        insert txm;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 00000000;
        Q.Quote_Number__c ='000000';
        Q.Approved__c = false;
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = lstPB[0].id;
        Q.Tax_Classification__c='IGST';
        Q.Discount_Amount__c = 100;
       
        
        insert Q;  
       
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id,SGST__c=6, CGST__c=6,IGST__c =0,Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL;  
        
            
        
    
    
    
    
        Project__c p = new Project__c(Name = 'NewProject',Project_Type__c = 'New PE',
        Opportunity__c = Opp[0].id,  
                       Quote__c = q.id);
        insert p;
        
        Product2 prod = new Product2(Name = 'ProdTest', CST__c = 2, Central_Excise__c = 3, VAT__c = 4, 
                        Family = '');
        insert prod; 
        
        Employee__c e = new Employee__c(Name = 'lakshmanan');
        insert e; 
               
        Project_Item__c pi = new Project_Item__c(Project__c = p.id,Status__c = 'In Progress',Product__c = prod.id,
                                 Technician_Member__c = 'suren; lakshmanan');
        insert pi;  

        
        QuoteLineItem qli = new QuoteLineItem (QuoteId = q.id,PricebookEntryId = lstPbentry[0].id, Quantity = 10,
                            UnitPrice = 100);
        insert qli;                    

        
         RawMaterial__c rm1 = new RawMaterial__c(Name = 'Painting Mask',Unit__c='MM');
        insert rm1;
       
	   
	    String orgId = UserInfo.getOrganizationId();
    String dateString = 
        String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
	
	
        
       Raw_Material_Inventory__c  RW= New Raw_Material_Inventory__c(Name='Painting Mask'+uniqueName,
       RawMaterial__c=rm1.ID,Min_Quantity_Check__c=True,Minimum_Stock_Quantity__c=100);
       
        insert  RW ;  
        
        Dispatched_Raw_Material__c DR= new Dispatched_Raw_Material__c(
        Raw_Material_Inventory__c=RW.ID);
        insert DR;      
         
         IssuedConsumable RMR = New IssuedConsumable();
         rmr.addRows();
         rmr.saveAccount();
          
    }
}

 
shakila Gshakila G
User-added image
Raj VakatiRaj Vakati
Set see all data to true and try 

@isTest(seealldata=true)


 
shakila Gshakila G
User-added image
Raj VakatiRaj Vakati
Its Duplicate issue try this one 
 
@isTest(seealldata=true)
Public class IssuedConsumable_test{
    static testMethod void IssuedConsumable_test(){
        
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        insert lstprod; 
        
        Integer indexvalue = 0;
        if(lstprod.size()>0)
        {
            for(pricebookentry irow :lststdPbe)
            {
                irow.product2id = lstprod[indexvalue].id;
                
                indexvalue++;
            }
        }
        
        
        insert lststdPbe;
        
        insert lstPB; 
        
        
        if(lstprod.size()>0)
        {
            for(pricebook2 irow :lstPB)
            {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);
                }
            }
        }

        insert lstPbentry;
        insert acc[0];
        
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        update Opp[0];
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
            pricebookentryid=lstPbentry[0].id);
        insert OL; 
        
     
        
        Tax_Master__c   txm= new Tax_Master__c(CGST__c=14.00,HSN_Code__c=lstprod[0].HSN_Code__c,IGST__c=28.00,SGST__c=14.00, Start_Date__c =  Date.newInstance(2017, 07, 06), End_Date__c = System.today()+2);
        insert txm;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 00000000;
        Q.Quote_Number__c ='000000';
        Q.Approved__c = false;
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = lstPB[0].id;
        Q.Tax_Classification__c='IGST';
        Q.Discount_Amount__c = 100;
       
        
        insert Q;  
       
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id,SGST__c=6, CGST__c=6,IGST__c =0,Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL;  
        
            
        
    
    
    
    
        Project__c p = new Project__c(Name = 'NewProject',Project_Type__c = 'New PE',
        Opportunity__c = Opp[0].id,  
                       Quote__c = q.id);
        insert p;
        
        Product2 prod = new Product2(Name = 'ProdTest', CST__c = 2, Central_Excise__c = 3, VAT__c = 4, 
                        Family = '');
        insert prod; 
        
        Employee__c e = new Employee__c(Name = 'lakshmanan');
        insert e; 
               
        Project_Item__c pi = new Project_Item__c(Project__c = p.id,Status__c = 'In Progress',Product__c = prod.id,
                                 Technician_Member__c = 'suren; lakshmanan');
        insert pi;  

        
        QuoteLineItem qli = new QuoteLineItem (QuoteId = q.id,PricebookEntryId = lstPbentry[0].id, Quantity = 10,
                            UnitPrice = 100);
        insert qli;                    

        
         RawMaterial__c rm1 = new RawMaterial__c(Name = 'Painting Mask',Unit__c='MM');
        insert rm1;
       
	   
	    String orgId = UserInfo.getOrganizationId();
    String dateString = 
        String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
	
	
        
       Raw_Material_Inventory__c  RW= New Raw_Material_Inventory__c(Name='This for TestClass'+uniqueName,
       RawMaterial__c=rm1.ID,Min_Quantity_Check__c=false,
	   Minimum_Stock_Quantity__c=3274);
       
        insert  RW ;  
        
        Dispatched_Raw_Material__c DR= new Dispatched_Raw_Material__c(
        Raw_Material_Inventory__c=RW.ID);
        insert DR;      
         
         IssuedConsumable RMR = New IssuedConsumable();
         rmr.addRows();
         rmr.saveAccount();
          
    }
}

 
shakila Gshakila G
Same Error
 
Raj VakatiRaj Vakati
Are you using update code ?? 
 
Raw_Material_Inventory__c RW= New Raw_Material_Inventory__c(Name='This for TestClass'+uniqueName,
       RawMaterial__c=rm1.ID,Min_Quantity_Check__c=false,
       Minimum_Stock_Quantity__c=3274);

 
shakila Gshakila G
....only Inserting.
=========
public class IssuedConsumable{

Dispatched_Raw_Material__c DPRM= new Dispatched_Raw_Material__c();

public list<Dispatched_Raw_Material__c> listAccount{get;set;}

{
listAccount=new list<Dispatched_Raw_Material__c>();
listAccount.add(DPRM);
}

Public void addRows()
{
Dispatched_Raw_Material__c acc = new Dispatched_Raw_Material__c();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Created Successfully'));    

}
Return page.IssuedConsumable;

}
}
faww afawfaww afaw
I have tested this script on my small content based wbsite. Its working fine. You can see the script demo on sniper paintball review (https://thebestpaintballgun.info/sniper-rifle) article.