You need to sign in to do that
Don't have an account?
Ammar P 9
Pls help me for code coverage.........
Apex class code:
public with sharing class TrackOrderController {
public string ordernum {get; set;}
public date orderDate {get; set;}
public List<Order> Order {get; set;}
public List<OrderItem> status {get; set;}
public List<string> vrs {public get; private set;}
public List<string> pro {public get; private set;}
public List<integer> index {public get; private set;}
public PageReference findOrder()
{
try{
Order = [SELECT id FROM Order where PoNumber=:ordernum and PoDate =: orderdate ];
status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: Order[0].Id];
system.debug('size-->'+status.size());
vrs = new List<String>();
index = new List<integer>();
for(integer i=0; i<status.size(); i++){
index.add(i);
if(status[i].Vehicle_Request_Status1__c != null){
vrs.add(status[i].Vehicle_Request_Status1__c);
}else{
vrs.add('Null');
}
}
system.debug(status);
system.debug('vrs list--->'+vrs);
system.debug('index list--->'+index);
pro = new List<string>();
for(integer i=0; i<status.size(); i++){
pro.add(status[i].Product_name__c);
}
system.debug('pro list--->'+pro);
return null;
}
catch(Exception ex){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please check purchase number and date')); return null; }
}
public PageReference reset() {
PageReference newpage = new PageReference(System.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
}
Test class:
@isTest(SeeAllData=true)
public class TrackOrderControllerTest {
static testMethod void findOrder(){
Account acc = New Account();
acc.name = 'test';
acc.CR_Number__c = '1234567890';
acc.CustomerName__c = 'test acc';
acc.CR_Date_English__c = Date.newInstance(2016, 12, 9);
acc.CR_Capital__c = 12426;
acc.Current_Revenue__c = 1233;
acc.Special_Admin_Rate__c = 123456;
acc.Security_Type__c = 'ghayth';
acc.ID_Issuer__c = 'asdasd';
acc.CR_City__c = 'asdasd';
acc.Workforce__c = 'sdadad';
acc.In_Business__c = 'sdasdasdas';
acc.Nationality_of_Business__c = 'India';
acc.Arabic_Date__c = Date.newInstance(2016, 10, 7);
acc.Company_Establishment__c = 'sdads';
acc.Bank2__c = 'sdfdsf';
acc.City_Region__c = 'sadf';
insert acc;
Product2 newProduct = new Product2();
newProduct.name = 'abc';
newProduct.Varient__c = 'asd';
newProduct.Make__c = 'AMI';
newProduct.Model__c = 'SEMI TRAILER';
newProduct.ModelYear__c = 2018;
newProduct.Fuel_Type__c = 'Petrol';
newProduct.Transmission__c = 'Powershift';
newProduct.Internal_Purchase_Price__c = 25;
newProduct.Registration_Fee__c = 25;
Insert newProduct;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(
Pricebook2Id = customPB.Id, Product2Id =newProduct.Id,
UnitPrice = 12000, IsActive = true);
insert customPrice;
Order ord = new Order ();
ord.AccountId =acc.Id;
ord.PoNumber='PO67543';
ord.PoDate = Date.newInstance(2016, 10, 5);
ord.Status ='Draft';
ord.EffectiveDate= system.today();
ord.Pricebook2Id = customPB.Id;
insert ord;
OrderItem orditem = new OrderItem();
orditem.OrderId = ord.Id;
Orditem.Vehicle_Request_Status1__c ='PO Approved';
orditem.PricebookEntryId =customPrice.Id;
orditem.UnitPrice= 678;
orditem.Quantity= 7;
insert orditem;
/* list<OrderItem>status=new List<OrderItem>();
list<integer>index=new List<integer>();
status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: ord.Id];
for(integer i=0; i<status.size(); i++){
index.add(i);
} */
PageReference testPage = Page.TrackOrder;
Test.setCurrentPage(testPage);
testPage.getParameters().put('Id', String.valueOf(ord.Id));
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(ord);
TrackOrderController testord = new TrackOrderController();
testord.findOrder();
testord.reset ();
Test.StopTest();
}
}
public with sharing class TrackOrderController {
public string ordernum {get; set;}
public date orderDate {get; set;}
public List<Order> Order {get; set;}
public List<OrderItem> status {get; set;}
public List<string> vrs {public get; private set;}
public List<string> pro {public get; private set;}
public List<integer> index {public get; private set;}
public PageReference findOrder()
{
try{
Order = [SELECT id FROM Order where PoNumber=:ordernum and PoDate =: orderdate ];
status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: Order[0].Id];
system.debug('size-->'+status.size());
vrs = new List<String>();
index = new List<integer>();
for(integer i=0; i<status.size(); i++){
index.add(i);
if(status[i].Vehicle_Request_Status1__c != null){
vrs.add(status[i].Vehicle_Request_Status1__c);
}else{
vrs.add('Null');
}
}
system.debug(status);
system.debug('vrs list--->'+vrs);
system.debug('index list--->'+index);
pro = new List<string>();
for(integer i=0; i<status.size(); i++){
pro.add(status[i].Product_name__c);
}
system.debug('pro list--->'+pro);
return null;
}
catch(Exception ex){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please check purchase number and date')); return null; }
}
public PageReference reset() {
PageReference newpage = new PageReference(System.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
}
Test class:
@isTest(SeeAllData=true)
public class TrackOrderControllerTest {
static testMethod void findOrder(){
Account acc = New Account();
acc.name = 'test';
acc.CR_Number__c = '1234567890';
acc.CustomerName__c = 'test acc';
acc.CR_Date_English__c = Date.newInstance(2016, 12, 9);
acc.CR_Capital__c = 12426;
acc.Current_Revenue__c = 1233;
acc.Special_Admin_Rate__c = 123456;
acc.Security_Type__c = 'ghayth';
acc.ID_Issuer__c = 'asdasd';
acc.CR_City__c = 'asdasd';
acc.Workforce__c = 'sdadad';
acc.In_Business__c = 'sdasdasdas';
acc.Nationality_of_Business__c = 'India';
acc.Arabic_Date__c = Date.newInstance(2016, 10, 7);
acc.Company_Establishment__c = 'sdads';
acc.Bank2__c = 'sdfdsf';
acc.City_Region__c = 'sadf';
insert acc;
Product2 newProduct = new Product2();
newProduct.name = 'abc';
newProduct.Varient__c = 'asd';
newProduct.Make__c = 'AMI';
newProduct.Model__c = 'SEMI TRAILER';
newProduct.ModelYear__c = 2018;
newProduct.Fuel_Type__c = 'Petrol';
newProduct.Transmission__c = 'Powershift';
newProduct.Internal_Purchase_Price__c = 25;
newProduct.Registration_Fee__c = 25;
Insert newProduct;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(
Pricebook2Id = customPB.Id, Product2Id =newProduct.Id,
UnitPrice = 12000, IsActive = true);
insert customPrice;
Order ord = new Order ();
ord.AccountId =acc.Id;
ord.PoNumber='PO67543';
ord.PoDate = Date.newInstance(2016, 10, 5);
ord.Status ='Draft';
ord.EffectiveDate= system.today();
ord.Pricebook2Id = customPB.Id;
insert ord;
OrderItem orditem = new OrderItem();
orditem.OrderId = ord.Id;
Orditem.Vehicle_Request_Status1__c ='PO Approved';
orditem.PricebookEntryId =customPrice.Id;
orditem.UnitPrice= 678;
orditem.Quantity= 7;
insert orditem;
/* list<OrderItem>status=new List<OrderItem>();
list<integer>index=new List<integer>();
status = [SELECT Product_name__c,Vehicle_Request_Status1__c FROM OrderItem WHERE OrderId =: ord.Id];
for(integer i=0; i<status.size(); i++){
index.add(i);
} */
PageReference testPage = Page.TrackOrder;
Test.setCurrentPage(testPage);
testPage.getParameters().put('Id', String.valueOf(ord.Id));
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(ord);
TrackOrderController testord = new TrackOrderController();
testord.findOrder();
testord.reset ();
Test.StopTest();
}
}
Hi Please find the solution.
Please do somme changes according to your code.
Please let me know it is working or not?
Please mark it as the best answer so that other people would take references from it.
Please try the below test class:
Thanks,
Maharajan.C