You need to sign in to do that
Don't have an account?
Rahul Chauhan 33
My requirement is Write a test class ....coverage 80% above
My apex code :-
public class Acctcontroller
{
// Delaration
public Account accobj{get;set;}
public Opportunity oppobj{get;set;}
public List<Product2> Productlist{get;set;}
public List<wrapperclass> wraplist{get;set;}
Public List<Pricebook2> pbookobj{get;set;}
Public Set<Id> productidset{get;set;}
public string oppid;
public List<Opportunity> pbookidlst{get;set;}
Public List<Pricebookentry> pbe{get;set;}
Public Map<Id,Double> mapunitprice{get;set;}
Public Map<Id,string> mapproduct{get;set;}
// constructor
public Acctcontroller() {
wraplist = new List < wrapperclass > ();
oppobj = new Opportunity();
pbookobj = new List < Pricebook2 > ();
String accId = ApexPages.currentPage().getParameters().get('Id');
System.debug('Accountid' + accId);
oppid = ApexPages.currentPage().getParameters().get('oppId');
if (accId != null && accId.trim() != '') {
accobj = [SELECT Id,
Name
FROM Account
WHERE Id = : accId];
System.debug('hey' + accobj);
oppobj.AccountId = accId;
}
Productlist = [SELECT Id,
Name, Productcode
FROM Product2];
System.debug('Plistvalue' + Productlist);
for (product2 prodt: Productlist) {
wrapperclass wrap = new wrapperclass();
wrap.pro = prodt;
wraplist.add(wrap);
}
}
public Pagereference save() {
list<pricebook2> istpbook = [SELECT Id,
Name
FROM pricebook2
LIMIT 1];
if(!istpbook.isEmpty())
oppobj.pricebook2id = istpbook[0].id;
insert oppobj;
Pagereference objPG = new Pagereference('/apex/opportunityproductline?oppid=' + oppobj.id);
objPG.setRedirect(true);
return objPG;
}
public Pagereference saveproduct() {
productidset = new set <Id>();
for (wrapperclass wss: wraplist) {
if (wss.checkbx == true) {
productidset.add(wss.pro.id);
}
}
System.debug('productsetid'+productidset);
pbookidlst = [SELECT Id,
Name,
Pricebook2id
FROM Opportunity
WHERE Id = :oppId];
System.debug('productset1'+pbookidlst);
pbe =[SELECT id,
UnitPrice,
Product2Id
FROM pricebookentry
WHERE product2.id in :productidset];
System.debug('pricebookunitprice'+pbe);
mapunitprice = new Map<id,Double>();
for(pricebookentry p : pbe)
{
mapunitprice.put(p.Product2Id,p.UnitPrice);
}
System.debug('Mapid'+mapunitprice);
list < OpportunityLineItem > lstOLI = new list < OpportunityLineItem > ();
for (wrapperclass ws: wraplist) {
if (ws.checkbx == true) {
OpportunityLineItem objOLI = new OpportunityLineItem();
objOLI.Product2Id = ws.pro.id;
objOLI.OpportunityId = oppid;
objOLI.UnitPrice=mapunitprice.get(ws.pro.id);
System.debug('prod id'+ws.pro.id+'\n--map-'+mapunitprice.get(ws.pro.id));
System.debug('id11'+ws.pro.id);
objOLI.Quantity = ws.qty;
System.debug('Quantity'+objOLI.Quantity);
System.debug('Tp'+objOLI.totalprice);
// objOLI.totalprice = 100;
//objOLI.UnitPrice=map.get(ws.pro.id);
lstOLI.add(objOLI);
}
}
insert lstOLI;
System.debug('waplistvalue' + lstOLI);
Pagereference objPGg = new Pagereference('https://ap.salesforce.com?id=accid');
objPGg.setRedirect(true);
return objPGg;
}
public class wrapperclass {
public product2 pro {get;set;}
public Boolean checkbx {get;set;}
public integer qty {get;set;}
// Wrapper Constructor
public wrapperclass() {
pro = new product2();
checkbx = false;
}
}
}
I am trying this :-
@isTest
public class Acctcontroller_Test {
static testMethod void Updatefield() {
Account acc = new Account();
acc.Name = 'Test';
acc.Rating = 'Hot';
acc.Status__c= 'Active';
insert acc;
Opportunity opp = new Opportunity();
opp.name = 'Test';
opp.AccountId = acc.Id;
opp.Amount =1500 ;
opp.StageName = 'Closed Won';
opp.CloseDate = Date.today();
opp.Type = 'New Customers';
insert opp;
Test.StartTest();
Acctcontroller refClass = new Acctcontroller ();
Test.stopTest();
}
}
but its coverage only 20%
Please help me.........
Thanx in adv.*
public class Acctcontroller
{
// Delaration
public Account accobj{get;set;}
public Opportunity oppobj{get;set;}
public List<Product2> Productlist{get;set;}
public List<wrapperclass> wraplist{get;set;}
Public List<Pricebook2> pbookobj{get;set;}
Public Set<Id> productidset{get;set;}
public string oppid;
public List<Opportunity> pbookidlst{get;set;}
Public List<Pricebookentry> pbe{get;set;}
Public Map<Id,Double> mapunitprice{get;set;}
Public Map<Id,string> mapproduct{get;set;}
// constructor
public Acctcontroller() {
wraplist = new List < wrapperclass > ();
oppobj = new Opportunity();
pbookobj = new List < Pricebook2 > ();
String accId = ApexPages.currentPage().getParameters().get('Id');
System.debug('Accountid' + accId);
oppid = ApexPages.currentPage().getParameters().get('oppId');
if (accId != null && accId.trim() != '') {
accobj = [SELECT Id,
Name
FROM Account
WHERE Id = : accId];
System.debug('hey' + accobj);
oppobj.AccountId = accId;
}
Productlist = [SELECT Id,
Name, Productcode
FROM Product2];
System.debug('Plistvalue' + Productlist);
for (product2 prodt: Productlist) {
wrapperclass wrap = new wrapperclass();
wrap.pro = prodt;
wraplist.add(wrap);
}
}
public Pagereference save() {
list<pricebook2> istpbook = [SELECT Id,
Name
FROM pricebook2
LIMIT 1];
if(!istpbook.isEmpty())
oppobj.pricebook2id = istpbook[0].id;
insert oppobj;
Pagereference objPG = new Pagereference('/apex/opportunityproductline?oppid=' + oppobj.id);
objPG.setRedirect(true);
return objPG;
}
public Pagereference saveproduct() {
productidset = new set <Id>();
for (wrapperclass wss: wraplist) {
if (wss.checkbx == true) {
productidset.add(wss.pro.id);
}
}
System.debug('productsetid'+productidset);
pbookidlst = [SELECT Id,
Name,
Pricebook2id
FROM Opportunity
WHERE Id = :oppId];
System.debug('productset1'+pbookidlst);
pbe =[SELECT id,
UnitPrice,
Product2Id
FROM pricebookentry
WHERE product2.id in :productidset];
System.debug('pricebookunitprice'+pbe);
mapunitprice = new Map<id,Double>();
for(pricebookentry p : pbe)
{
mapunitprice.put(p.Product2Id,p.UnitPrice);
}
System.debug('Mapid'+mapunitprice);
list < OpportunityLineItem > lstOLI = new list < OpportunityLineItem > ();
for (wrapperclass ws: wraplist) {
if (ws.checkbx == true) {
OpportunityLineItem objOLI = new OpportunityLineItem();
objOLI.Product2Id = ws.pro.id;
objOLI.OpportunityId = oppid;
objOLI.UnitPrice=mapunitprice.get(ws.pro.id);
System.debug('prod id'+ws.pro.id+'\n--map-'+mapunitprice.get(ws.pro.id));
System.debug('id11'+ws.pro.id);
objOLI.Quantity = ws.qty;
System.debug('Quantity'+objOLI.Quantity);
System.debug('Tp'+objOLI.totalprice);
// objOLI.totalprice = 100;
//objOLI.UnitPrice=map.get(ws.pro.id);
lstOLI.add(objOLI);
}
}
insert lstOLI;
System.debug('waplistvalue' + lstOLI);
Pagereference objPGg = new Pagereference('https://ap.salesforce.com?id=accid');
objPGg.setRedirect(true);
return objPGg;
}
public class wrapperclass {
public product2 pro {get;set;}
public Boolean checkbx {get;set;}
public integer qty {get;set;}
// Wrapper Constructor
public wrapperclass() {
pro = new product2();
checkbx = false;
}
}
}
I am trying this :-
@isTest
public class Acctcontroller_Test {
static testMethod void Updatefield() {
Account acc = new Account();
acc.Name = 'Test';
acc.Rating = 'Hot';
acc.Status__c= 'Active';
insert acc;
Opportunity opp = new Opportunity();
opp.name = 'Test';
opp.AccountId = acc.Id;
opp.Amount =1500 ;
opp.StageName = 'Closed Won';
opp.CloseDate = Date.today();
opp.Type = 'New Customers';
insert opp;
Test.StartTest();
Acctcontroller refClass = new Acctcontroller ();
Test.stopTest();
}
}
but its coverage only 20%
Please help me.........
Thanx in adv.*
All Answers
The reason you are only getting 20% is because you are not calling your other methods in your test class. You need to call both the save() and saveproduct() methods to get the rest of your coverrage.
Also, please considering using a test setup (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm) to create your base line test data instead of in your test method.