i have following Controller of select list and select option.i just make the test class of that controller but i am not covered the whole part of the controller.here is my controller,test class and snapshot of testclass that covered...
Public Class ObjectRelation {
Public List<Product2> pro{get;set;}
public List<SelectOption> AccountOptions {get;set;}
public List<SelectOption> OpportunityOptions{get;set;}
public List<SelectOption> ProductOptions{get;set;}
Public String AccName{get;set;}
Public String OppName{get;set;}
Public String prodName{get;set;}
Public ObjectRelation()
{
AccountOptions = new List<SelectOption>();
AccountOptions.add(new SelectOption('none','----Select Account----'));
for (Account record : [select Id, Name from Account])
{
AccountOptions.add(new SelectOption(record.Id,record.Name));
}
}
Public List<SelectOption> getOpp(){
OpportunityOptions = new List<SelectOption>();
OpportunityOptions.add(new SelectOption('none','----Select Opportunity----'));
for (Opportunity record : [select id,name from Opportunity where Account.Id =: AccName] )
{
OpportunityOptions.add(new SelectOption(record.Id,record.Name));
}
System.Debug('####'+OpportunityOptions);
return OpportunityOptions;
}
Public List<SelectOption> getProd(){
ProductOptions = new List<SelectOption>();
ProductOptions.add(new SelectOption('none','----Select Product----'));
for (OpportunityLineItem record : [SELECT Id,PricebookEntry.Product2Id,PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId =: OppName] )
{
ProductOptions.add(new SelectOption(record.PricebookEntry.Product2Id,record.PricebookEntry.Product2.Name));
}
return ProductOptions;
}
Public List<Product2> GetDisplayProduct(){
pro = new List<Product2>();
pro = [Select id,Name,ProductCode,Description,IsActive from Product2 where id =: prodName ];
if(OppName == '0069000000DY2nV'){
System.debug('@'+prodName);
}
else{
System.debug('@@'+prodName);
}
string sString = 'some*Stuff*vivek';
return pro;
}
public PageReference Save(){
try{
if(pro.size() > 0){
upsert pro;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Record Successfully Updated'));
}
} catch (DMLException e){
// show the messages to the user
ApexPages.addMessages(e);
}
return null;
}
}
Test Class of the Above Controller
@isTest(SeeAllData=true)
Public class TestObjectRelation {
Static TestMethod void TestObjectRelation (){
PageReference pageRef = Page.WrapperStandard;
Test.setCurrentPage(pageRef);
ObjectRelation objMain = new ObjectRelation();
Account acc = new Account(name='vivek113');
insert acc;
Opportunity opp = new Opportunity(AccountId=acc.id,name='opp',StageName='abc',CloseDate=System.today(),optname__c='opp');
insert opp;
/* List<SelectOption> OpportunityOptions = new List<SelectOption>();
Opportunity record = [select id,name from Opportunity where Account.Id =: AccName];
OpportunityOptions.add(new SelectOption(record.Id,record.Name));*/
Product2 prod = new Product2(name='new',IsActive = true);
insert prod;
Pricebook2 objprice = [select Id from PriceBook2 where IsStandard=True];
System.debug('****'+objprice);
objprice.IsActive = true;
update objprice;
PricebookEntry standardPrice= new PricebookEntry (Product2Id = prod.id,unitprice=20,Pricebook2Id = objprice.id,UseStandardPrice=false,isActive=true);
insert standardPrice;
OpportunityLineItem objoppline = new OpportunityLineItem (OpportunityId=opp.id,pricebookentryId=standardprice.id,Quantity=10,totalprice=40);
insert objoppline;
objMain.getopp();
objMain.getprod();
objMain.getdisplayProduct();
objMain.save();
}
}
Snapshot to cover...
Here now help me to cover the red part of the test class....Thanks for advance...Please....