You need to sign in to do that
Don't have an account?
Amit Kr Singh
How to write test class for this class
My controller:-
public class Auditcontroller
{
public Auditcontroller(ApexPages.StandardController controller) {
inventAudit=new Inventory_Audit__c();
productcount=new List<Product_Count__c>();
poid=ApexPages.currentPage().getParameters().get('id');
productcount=new List<Product_Count__c>();
if(poid !=null)
{
inventAudit=[select name,Account__c,Status__c,Audit_Date__c,product__c from Inventory_Audit__c where id=:poid];
}
ps=inventAudit.Account__c;
p=new Product2();
}
public Product2 p{get;set;}
public list<Account> acc{get;set;}
public list<Asset> invent{get;set;}
public Inventory_Audit__c inventAudit{get;set;}
public List<Product_Count__c> productcount;
public List<InventWrapper> lstInventWrapper {get;set;}
public Product_Count__c pc{get;set;}
Public string sp{get;set;}
public Integer call{get;set;}
public Id stockdcid {get;set;}
public String currentStockInventory{get;set;}
Public Id Poid{get;set;}
public Asset ass{get;set;}
public string ps;
public void productDetails() //on click 'Go' button based on seach condition displaying inventory details on page
{
if( inventAudit.Account__c!=null && inventAudit.Status__c=='open')
{
invent=[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
currentStockInventory=apexpages.currentpage().getparameters().get('sfdc.tabName');
inventAudit.Status__c='Open';
// insert inventAudit;
lstInventWrapper =new List<InventWrapper> ();
for(Asset indetail:invent)
{
InventWrapper w = new InventWrapper();
w.ind=indetail;
//w.PhysicalCount = null;
w.Comments='';
w.Reason='-None-';
lstInventWrapper.add(w);
System.debug('The list value@@@@@@@@@@@@@@@: ' + lstInventWrapper);
}
}
else
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Select the search condition'));
}
}
Public void Saveasset()
{
string sid=inventAudit.id;
System.debug('The value is@@@@@@@@@@@@@@@: ' + lstInventWrapper.size());
for(Integer i=0;i<lstInventWrapper.size();i++)
{
pc=new Product_Count__c (Name=lstInventWrapper[i].ind.name ,Asset_Barcode__c=lstInventWrapper[i].ind.SerialNumber,Inventory_Audit__c=inventAudit.Id,Product__c=lstInventWrapper[i].ind.Product2Id,Reasons__c=lstInventWrapper[i].ind.Reason1__c,Asset__c=lstInventWrapper[i].ind.id);
productcount.add(pc);
}
upsert productcount;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record entered Successfully.Thank you!'));
}
public class InventWrapper
{
public Asset ind {get;set;}
// public Integer PhysicalCount{get;set;}
public String Comments{get;set;}
public String Reason{get;set;}
}
}
My Testclass:-
@isTest
Private class Auditcontroller_Test
{
static testMethod void Testme()
{
PageReference pageRef = Page.Missmatch;
Test.setCurrentPage(pageRef);
list<Account> acc1=new list<Account>();
list<Asset> invent1=new list<Asset>();
string sp;
sp='34563';
Integer call;
Id stockdcid;
String currentStockInventory;
Id Poid;
string b;
b='12345';
RecordType RecType = [Select Id, Name, DeveloperName From RecordType Where SobjectType =: 'Account' and name=:'Dealer'];
Account acc= new Account();
acc.Name = 'Test Account';
acc.RecordTypeid = RecType.id;
acc.Email_Address_POC__c='aksingh028@gmail.com';
acc.Phone = '7676716272';
acc.Vat__c=10;
acc.No_of_Asset__c=20;
insert acc;
Contact cc = new Contact();
cc.LastName = 'Spiderman';
cc.Email = 'spiderman@gmail.com';
cc.AccountId= acc.Id;
insert cc;
Product2 prod = new Product2(Name = 'Laptop X200',
Family = 'Standrad');
insert prod;
Asset asst=new Asset();
asst.Name='Test Assest';
asst.AccountId=acc.id;
asst.ContactId=cc.id;
asst.Product2Id=prod.id;
asst.Audit__c='Not present';
//asst.Purchase_Order__c=po.id;
Insert asst;
Inventory_Audit__c invaudit= new Inventory_Audit__c();
//invaudit.Account__c=acc.id;
//invaudit.Status__c='Approved';
invaudit.Status__c='Open';
invaudit.Audit_Date__c=system.today();
invaudit.Product__c=prod.id;
Insert invaudit;
ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
string cid1=ApexPages.currentPage().getParameters().get('id');
invaudit.Status__c='open';
List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();
lstcon.add(invaudit);
Product_Count__c pc= new Product_Count__c();
pc.Name='testpc';
pc.Asset__c=asst.id;
pc.Inventory_Audit__c=invaudit.id;
insert pc;
Test.startTest();
Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
invwrapper.Reason= 'None';
invwrapper.Comments= 'hi';
invwrapper.ind=asst;
AuditCon.productDetails();
//Asset a =[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
//AuditCon.Saveasset();
Test.stopTest();
}
}
Please help me where i am doing mistake only i am getting 39% code coverage
please help me
public class Auditcontroller
{
public Auditcontroller(ApexPages.StandardController controller) {
inventAudit=new Inventory_Audit__c();
productcount=new List<Product_Count__c>();
poid=ApexPages.currentPage().getParameters().get('id');
productcount=new List<Product_Count__c>();
if(poid !=null)
{
inventAudit=[select name,Account__c,Status__c,Audit_Date__c,product__c from Inventory_Audit__c where id=:poid];
}
ps=inventAudit.Account__c;
p=new Product2();
}
public Product2 p{get;set;}
public list<Account> acc{get;set;}
public list<Asset> invent{get;set;}
public Inventory_Audit__c inventAudit{get;set;}
public List<Product_Count__c> productcount;
public List<InventWrapper> lstInventWrapper {get;set;}
public Product_Count__c pc{get;set;}
Public string sp{get;set;}
public Integer call{get;set;}
public Id stockdcid {get;set;}
public String currentStockInventory{get;set;}
Public Id Poid{get;set;}
public Asset ass{get;set;}
public string ps;
public void productDetails() //on click 'Go' button based on seach condition displaying inventory details on page
{
if( inventAudit.Account__c!=null && inventAudit.Status__c=='open')
{
invent=[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
currentStockInventory=apexpages.currentpage().getparameters().get('sfdc.tabName');
inventAudit.Status__c='Open';
// insert inventAudit;
lstInventWrapper =new List<InventWrapper> ();
for(Asset indetail:invent)
{
InventWrapper w = new InventWrapper();
w.ind=indetail;
//w.PhysicalCount = null;
w.Comments='';
w.Reason='-None-';
lstInventWrapper.add(w);
System.debug('The list value@@@@@@@@@@@@@@@: ' + lstInventWrapper);
}
}
else
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Select the search condition'));
}
}
Public void Saveasset()
{
string sid=inventAudit.id;
System.debug('The value is@@@@@@@@@@@@@@@: ' + lstInventWrapper.size());
for(Integer i=0;i<lstInventWrapper.size();i++)
{
pc=new Product_Count__c (Name=lstInventWrapper[i].ind.name ,Asset_Barcode__c=lstInventWrapper[i].ind.SerialNumber,Inventory_Audit__c=inventAudit.Id,Product__c=lstInventWrapper[i].ind.Product2Id,Reasons__c=lstInventWrapper[i].ind.Reason1__c,Asset__c=lstInventWrapper[i].ind.id);
productcount.add(pc);
}
upsert productcount;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record entered Successfully.Thank you!'));
}
public class InventWrapper
{
public Asset ind {get;set;}
// public Integer PhysicalCount{get;set;}
public String Comments{get;set;}
public String Reason{get;set;}
}
}
My Testclass:-
@isTest
Private class Auditcontroller_Test
{
static testMethod void Testme()
{
PageReference pageRef = Page.Missmatch;
Test.setCurrentPage(pageRef);
list<Account> acc1=new list<Account>();
list<Asset> invent1=new list<Asset>();
string sp;
sp='34563';
Integer call;
Id stockdcid;
String currentStockInventory;
Id Poid;
string b;
b='12345';
RecordType RecType = [Select Id, Name, DeveloperName From RecordType Where SobjectType =: 'Account' and name=:'Dealer'];
Account acc= new Account();
acc.Name = 'Test Account';
acc.RecordTypeid = RecType.id;
acc.Email_Address_POC__c='aksingh028@gmail.com';
acc.Phone = '7676716272';
acc.Vat__c=10;
acc.No_of_Asset__c=20;
insert acc;
Contact cc = new Contact();
cc.LastName = 'Spiderman';
cc.Email = 'spiderman@gmail.com';
cc.AccountId= acc.Id;
insert cc;
Product2 prod = new Product2(Name = 'Laptop X200',
Family = 'Standrad');
insert prod;
Asset asst=new Asset();
asst.Name='Test Assest';
asst.AccountId=acc.id;
asst.ContactId=cc.id;
asst.Product2Id=prod.id;
asst.Audit__c='Not present';
//asst.Purchase_Order__c=po.id;
Insert asst;
Inventory_Audit__c invaudit= new Inventory_Audit__c();
//invaudit.Account__c=acc.id;
//invaudit.Status__c='Approved';
invaudit.Status__c='Open';
invaudit.Audit_Date__c=system.today();
invaudit.Product__c=prod.id;
Insert invaudit;
ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
string cid1=ApexPages.currentPage().getParameters().get('id');
invaudit.Status__c='open';
List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();
lstcon.add(invaudit);
Product_Count__c pc= new Product_Count__c();
pc.Name='testpc';
pc.Asset__c=asst.id;
pc.Inventory_Audit__c=invaudit.id;
insert pc;
Test.startTest();
Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
invwrapper.Reason= 'None';
invwrapper.Comments= 'hi';
invwrapper.ind=asst;
AuditCon.productDetails();
//Asset a =[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
//AuditCon.Saveasset();
Test.stopTest();
}
}
Please help me where i am doing mistake only i am getting 39% code coverage
please help me
PLease try below code.
Let us know if this will help you
All Answers
Now i am gettting 45% only code coverage.. plz help me
Time Started2/5/2016 12:06 PM
ClassAuditcontroller_Test
Method NameTestme
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Account__c]
Stack TraceClass.Auditcontroller_Test.Testme: line 76, column 1
Post your test class which you are using
Private class Auditcontroller_Test
{
static testMethod void Testme()
{
list<Account> acc1=new list<Account>();
list<Asset> invent1=new list<Asset>();
string sp;
sp='34563';
Integer call;
Id stockdcid;
String currentStockInventory;
Id Poid;
string b;
b='12345';
RecordType RecType = [Select Id, Name, DeveloperName From RecordType Where SobjectType =: 'Account' and name=:'Dealer'];
Account acc= new Account();
acc.Name = 'Test Account';
acc.RecordTypeid = RecType.id;
acc.Email_Address_POC__c='aksingh028@gmail.com';
acc.Phone = '7676716272';
acc.Vat__c=10;
acc.No_of_Asset__c=20;
insert acc;
Contact cc = new Contact();
cc.LastName = 'Spiderman';
cc.Email = 'spiderman@gmail.com';
cc.AccountId= acc.Id;
insert cc;
Product2 prod = new Product2(Name = 'Laptop X200',
Family = 'Standrad');
insert prod;
Asset asst=new Asset();
asst.Name='Test Assest';
asst.AccountId=acc.id;
asst.ContactId=cc.id;
asst.Product2Id=prod.id;
asst.Audit__c='Not present';
Insert asst;
Inventory_Audit__c invaudit= new Inventory_Audit__c();
//invaudit.Account__c=acc.id;
invaudit.Status__c='Open';
invaudit.Audit_Date__c=system.today();
invaudit.Product__c=prod.id;
Insert invaudit;
ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
ApexPages.CurrentPage().getparameters().put('sfdc.tabName','Test');
invaudit.Status__c='open';
List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();
lstcon.add(invaudit);
Product_Count__c pc= new Product_Count__c();
pc.Name='testpc';
pc.Asset__c=asst.id;
pc.Inventory_Audit__c=invaudit.id;
insert pc;
Test.startTest();
PageReference pageRef = Page.Missmatch;
Test.setCurrentPage(pageRef);
Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
invwrapper.Reason= 'None';
invwrapper.Comments= 'hi';
invwrapper.ind=asst;
try
{
AuditCon.productDetails();
AuditCon.Saveasset();
}
Catch(Exception ee)
{}
Test.stopTest();
}
}
==================================================================
Invoice
Inventory_Audit__c invaudit= new Inventory_Audit__c();
//invaudit.Account__c=acc.id;
i have comment it then i am getting 43% plz tell me where i am doing mistake
It look like your lookup ia filter lookup. Please post the screen shot of account lookup.
Plz Check now
Object->Inventory_Audit__c --> Account__c
Please post account lookup screen shot
Please help me
plz check now..
Please follow below step :-
Object->Inventory_Audit__c --> Account__c
Then click on account lookup field then post screen shot
Screen shot for Account lookup ...Plz help me
PLease try below code.
Let us know if this will help you