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
VidyaVidya 

Help needed in writing test class

Pls help me to write a test class for the below class.

 

 

public class transferinnew1 {
String pid = ApexPages.currentPage().getParameters().get('id');
public  List<Product_serial__c> pserial=new List<Product_Serial__c>();
);

public  List<Transfer_In__c> pt=new List<Transfer_in__c>();

 
 public transferinnew1(){


 pserial=[select id,name,Product_disp__c,type__c,serial_number__c,status__c,warehouse__c,bin__c,order__c,select__c,project__c
from product_serial__c where status__c='Out'  order by order__c asc];
 
 
 pt=[select id,reasons__c ,order__c from transfer_in__c where id =:ApexPages.currentPage().getParameters().get('id') ];

}

public List<product_serial__c> getRecord() {
            return pserial;
      }
      public List<transfer_in__c> getRec() {
            return pt;
      }

    public PageReference save() {
   
      
  update pserial;
for ( product_serial__c ps:pserial){

  if(ps.select__c==true){
  Transfer_In_Line__c iv = new Transfer_In_Line__c(product_serial__c = ps.id,
        transfer_in__c=pid,warehouse__c=ps.warehouse__c,bin__c=ps.bin__c );
insert iv;
ps.select__c=false;
ps.status__c='Available';
  ps.tid__c=pt[0].reasons__c;      
update ps;
if (pt[0].reasons__c=='Excess Material')
{
ps.order_stock__c=false;
ps.type__c='Spare Stock';
ps.order__c=null;
update ps;
}
if (pt[0].reasons__c=='Ongoing Project (Hold Stock)')
{
ps.order_stock__c=true;
ps.order__c=pt[0].order__c;
ps.status__c='Order Stock';
ps.type__c='Spare Stock';

update ps;
}

}
 
}

 
  Pagereference p = new PageReference('/'+pid);

p.setRedirect(true);

return p;  
   
   }   
   public PageReference Cancel() {
  delete pt;

PageReference mysample = Page.tabinv;
       mysample.setRedirect(true);    
                     return mysample;
 

}


}

 

 

 

 

Thanks,

 

Vidya

 

 

minkeshminkesh

Hello vidya,

                    here is the code of test class.try it and let me know if it works for you.

 

Static testMethod void testcase(){
	transfer_in__c transferIn = new transfer_in__c();
	transferIn.Name = 'new Transfer';
	insert transferIn;
	Product_serial__c product = new Product_serial__c();
	product.Name = 'new Product';
	product.status__c = 'out';
	insert product;
	ApexPages.currentPage().getParameters().put('id',transferIn.Id);
	transferinnew1 transfer = new transferinnew1();
	transfer.getRecord();
	transfer.getRec();
	transfer.save();
}

 

 

VidyaVidya

Hi Minkesh,

 

Thanks for the effort.I am getting only 44% code coverage with the abovetest class.Pls help me once again..

 

Thanks,

 

Vidya

 

minkeshminkesh

Hello vidya,

 

can you tell how much code is covered and how much code is not covered so that i can help you.

vck01vck01

Try the following..Though i dint follow any standards in the below code..it should give an idea of how to cover the code for testing different scenarios.Always make sure to include the requried test data before actually testing a funcitonality and also make sure to write different test methods to test all the 'if'/'else' code .Assertion is a good practise to make sure consistency exists in code

 

//Start

static testmethod void Testtransferinnew1

 {       

                //Prepare test data

 product_serial__c pS = new product_serial__c(status__c='Out' ,Name='Test' );

                insert pS;

 

  transfer_in__c tsIn1= new transfer_in__c(reasons__c='Excess Material') ;

  insert tsIn1;

 

               Test.startTest();

 ApexPages.currentpage().getPAramerters().put('id',tsIn1.id);

 transferinnew1 tsN1 = new transferinnew1();

 tsN1.save();

 tsN1.Cancel();

 tsN1.getRec();

 tsN1.getRecords();

 

 

 Test.stoptest();

 

 }

 

//Start

static testmethod void Testtransferinnew2

{       

                //Prepare test data

  product_serial__c pS = new product_serial__c(status__c='Out' ,Name='Test' );

                insert pS;

 

                transfer_in__c tsIn2= new transfer_in__c(reasons__c='Ongoing Project (Hold Stock)') ;

                insert tsIn2;

 ApexPages.currentpage().getPAramerters().put('id',tsIn2.id);

 

               Test.startTest();

 

  transferinnew1 tsN1 = new transferinnew1();

  tsN1.save();

  tsN1.Cancel();

  tsN1.getRec();

  tsN1.getRecords();

 

Test.stoptest();

 

}

 

 

Hope this works!

 

Regards,

Chaitanya 

http://vck1.blogspot.com/

VidyaVidya

I have tried like the below.I commented the 4 line because it shows some errors.

 

Static testMethod void myTest(){
    Transfer_In__c transferIn = new Transfer_In__c();
    transferIn.Reason__c= 'Excess Material';
    insert transferIn;
    //Product_serial__c product = new Product_serial__c();
    //product.Name = 'new Product';
    //product.status__c = 'out';
    //insert product;
    ApexPages.currentPage().getParameters().put('id',transferIn.Id);
    transferinnew1 transfer = new transferinnew1();
    transfer.getRecord();
    transfer.getRec();
    transfer.save();
}

 

 

i got 44% from the above.

 

Part not covered

 

for ( product_serial__c ps:pserial){

  if(ps.select__c==true){
  Transfer_In_Line__c iv = new Transfer_In_Line__c(product_serial__c = ps.id,
        transfer_in__c=pid,warehouse__c=ps.warehouse__c,bin__c=ps.bin__c );
insert iv;
ps.select__c=false;
ps.status__c='Available';
  ps.tid__c=pt[0].reasons__c;      
update ps;
if (pt[0].reasons__c=='Excess Material')
{
ps.order_stock__c=false;
ps.type__c='Spare Stock';
ps.order__c=null;
update ps;
}
if (pt[0].reasons__c=='Ongoing Project (Hold Stock)')
{
ps.order_stock__c=true;
ps.order__c=pt[0].order__c;
ps.status__c='Order Stock';
ps.type__c='Spare Stock';

update ps;
}

}
 
}

 

 

and also the  mysample.setRedirect(true);

 

minkeshminkesh

Hello vidya,

                        try this one:-

\

Static testMethod void testcase(){
	transfer_in__c transferIn = new transfer_in__c();
	transferIn.Name = 'new Transfer';
	insert transferIn;
	Product_serial__c product = new Product_serial__c();
	product.Name = 'new Product';
	product.status__c = 'Out';
        product.select__c = true;
	insert product;
	ApexPages.currentPage().getParameters().put('id',transferIn.Id);
	transferinnew1 transfer = new transferinnew1();
        transfer.save();
	transfer.getRecord();
	transfer.getRec();
        transfer.cancel();
	
}

 

 

VidyaVidya

Hii Minkesh,

 

Sorry for the delayed reply.

 

The above code is also not working. On running test  it shows one  error like "EXCEPTION_THROWN|[101]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product_Inventory__c]: [Product_Inventory__c]".

 

 

 


Thanks

 

Vidya

 

VidyaVidya

Hi Chaitanya,

 

Thank you for posting a reply.

 

  I tried your code also.i am getting the same error like

On running test  it shows one  error like "EXCEPTION_THROWN|[101]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product_Inventory__c]: [Product_Inventory__c]".

 

Thanks,

 

Vidya

 


 


 

minkeshminkesh

Hello vidya,

                       thisd field is required so you need to enter value for this field and then run the test again.