You need to sign in to do that
Don't have an account?

Test class Code Coverage..............urgent Requirement............
Hi frnds can you help me out...!
below i write test class but it gives only 70% how to increase more than 75%
code in "bold" is not include in code coverage
==================controller==================
public with sharing class multipleRecordsOnOpportunity {
Public List<string> leftselected{get;set;}
public List<String> selectedMarketsList {get; set;}
Public List<string> rightselected{get;set;}
public list<selectoption> getPickLstValue(){return null;}
public Set<String> originalvalues = new Set<String>{'A','B','C','D','E','F','G','H','I'};
public set<string> leftvalues = new set<string>();
public set<string> rightvalues = new set<string>();
public account acc{set;get;}
public opportunity opp {set;get;}
public boolean flag{set;get;}
public list<opportunity> oppsToCreateList{set;get;}
// constructor
public multipleRecordsOnOpportunity(ApexPages.StandardController controller) {
opp= new opportunity();
leftselected = new List<String>();
rightselected = new List<String>();
leftvalues.addall(originalvalues );
selectedMarketsList= new list<string>();
oppsToCreateList=new list<Opportunity>();
rightselected = new List<String>();
string str=ApexPages.CurrentPage().getParameters().get('id');
system.debug('99999999999999999999999999999'+str);
try{
acc=[select id,name,CustomerPriority__c from account where id=:str limit 1];
if(acc.CustomerPriority__c=='High'|| acc.CustomerPriority__c=='Medium'){
flag=true;
}
else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Multiple Opportunities can only be created from High and medium from parent account.');
ApexPages.addMessage(myMsg);
}
}
catch(Exception e){
ApexPages.addMessages(e);
}
}
// after click on add buttone display vlaues in Right side panel
public List<SelectOption> getSelectedValues(){
List<SelectOption> options1 = new List<SelectOption>();
List<string> tempList = new List<String>();
tempList.addAll(rightvalues);
tempList.sort();
for(String s : tempList)
options1.add(new SelectOption(s,s));
system.debug('1111111111111111111111'+options1);
return options1;
}
// by defult left side values or after click on remove values in left panel
public List<SelectOption> getunselectedvalues(){
List<SelectOption> options = new List<SelectOption>();
/*Schema.DescribeFieldResult fieldResult = opportunity.opportunity_multi__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry f : ple){
if(f.getValue() != 'A' && f.getValue() != 'b' ){
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
}*/
List<string> tempList = new List<String>();
tempList.addAll(leftvalues);
tempList.sort();
for(string s : tempList)
options.add(new SelectOption(s,s));
system.debug('00000000000000000000000000000'+options);
return options;
}
// aaction function of add button add values in right side
public PageReference selectclick(){
rightselected.clear();
for(String s : leftselected){
leftvalues.remove(s);
rightvalues.add(s);
}
system.debug('22222222222222222222222'+leftvalues);
system.debug('33333333333333333333333'+rightvalues);
return null;
}
// after click on Remove button display vlaues in panel
public PageReference unselectclick(){
leftselected.clear();
for(String s : rightselected){
rightvalues.remove(s);
leftvalues.add(s);
}
system.debug('4444444444444444444444444'+leftvalues);
system.debug('5555555555555555555555555'+rightvalues);
return null;
}
// clone Records Based number of markets selecting(Ex:selecting c,d,e clones 3 records)
public PageReference updateMarkets(){
oppsToCreateList.clear();
selectedMarketsList.clear();
selectedMarketsList.addall(rightvalues);
system.debug('000000000000000000000'+rightvalues+selectedMarketsList);
for (Integer i=0; i<selectedMarketsList.size(); i++){
String themkt = selectedMarketsList.get(i);
boolean alreadyAdded = false;
//## see if the Market is already in the oppsToCreateList
for (Opportunity tmpopp : oppsToCreateList)
{
if (themkt == tmpopp.opportunity_multi__c)
{
alreadyAdded = true;
}
}
if (themkt == opp.opportunity_multi__c || alreadyAdded)
{
//## don't re-add the original opp to the list, and don't
//## add the same record to the list multiple times.
}
else
{
Opportunity newopp = opp.clone(false);
Newopp.OrderNumber__c = themkt;
newopp.accountid=acc.id;
newopp.ownerId=UserInfo.getUserId();
newopp.stagename='1 - Prospect Evaluation';
newopp.closedate=date.today();
newopp.Name =acc.name;
oppsToCreateList.add(newopp);
}
}
system.debug('1111111111111111111111111111111111111'+oppsToCreateList);
return null;
}
public PageReference SaveRecords(){
if (oppsToCreateList != null && oppsToCreateList.size() > 0)
{
try
{
insert oppsToCreateList;
}
catch (Exception e)
{
ApexPages.addMessages(e);
return null;
}
}
return new PageReference('/'+acc.Id);
}
}
==============Test class=============================================
@isTest()
public class TestmultipleRecordsOnOpportunity{
static testMethod void myUnitTest_creat_multiple_Opportunity()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c ='mp';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptions=myPageCon.getSelectedValues();
mypagecon.unselectclick();
List<SelectOption> testoptions11=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptionss=myPageCon.getSelectedValues();
list<String> originalvalues = new list<String>{'A','B','C'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_creat_multiple_Opportunity1()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='medium';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='High';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.selectedMarketsList =originalvalues ;
myPageCon.updateMarkets();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail1()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='low';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+null);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
//myPageCon.selectedMarketsList= 'UK;France';
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
}
below i write test class but it gives only 70% how to increase more than 75%
code in "bold" is not include in code coverage
==================controller==================
public with sharing class multipleRecordsOnOpportunity {
Public List<string> leftselected{get;set;}
public List<String> selectedMarketsList {get; set;}
Public List<string> rightselected{get;set;}
public list<selectoption> getPickLstValue(){return null;}
public Set<String> originalvalues = new Set<String>{'A','B','C','D','E','F','G','H','I'};
public set<string> leftvalues = new set<string>();
public set<string> rightvalues = new set<string>();
public account acc{set;get;}
public opportunity opp {set;get;}
public boolean flag{set;get;}
public list<opportunity> oppsToCreateList{set;get;}
// constructor
public multipleRecordsOnOpportunity(ApexPages.StandardController controller) {
opp= new opportunity();
leftselected = new List<String>();
rightselected = new List<String>();
leftvalues.addall(originalvalues );
selectedMarketsList= new list<string>();
oppsToCreateList=new list<Opportunity>();
rightselected = new List<String>();
string str=ApexPages.CurrentPage().getParameters().get('id');
system.debug('99999999999999999999999999999'+str);
try{
acc=[select id,name,CustomerPriority__c from account where id=:str limit 1];
if(acc.CustomerPriority__c=='High'|| acc.CustomerPriority__c=='Medium'){
flag=true;
}
else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Multiple Opportunities can only be created from High and medium from parent account.');
ApexPages.addMessage(myMsg);
}
}
catch(Exception e){
ApexPages.addMessages(e);
}
}
// after click on add buttone display vlaues in Right side panel
public List<SelectOption> getSelectedValues(){
List<SelectOption> options1 = new List<SelectOption>();
List<string> tempList = new List<String>();
tempList.addAll(rightvalues);
tempList.sort();
for(String s : tempList)
options1.add(new SelectOption(s,s));
system.debug('1111111111111111111111'+options1);
return options1;
}
// by defult left side values or after click on remove values in left panel
public List<SelectOption> getunselectedvalues(){
List<SelectOption> options = new List<SelectOption>();
/*Schema.DescribeFieldResult fieldResult = opportunity.opportunity_multi__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry f : ple){
if(f.getValue() != 'A' && f.getValue() != 'b' ){
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
}*/
List<string> tempList = new List<String>();
tempList.addAll(leftvalues);
tempList.sort();
for(string s : tempList)
options.add(new SelectOption(s,s));
system.debug('00000000000000000000000000000'+options);
return options;
}
// aaction function of add button add values in right side
public PageReference selectclick(){
rightselected.clear();
for(String s : leftselected){
leftvalues.remove(s);
rightvalues.add(s);
}
system.debug('22222222222222222222222'+leftvalues);
system.debug('33333333333333333333333'+rightvalues);
return null;
}
// after click on Remove button display vlaues in panel
public PageReference unselectclick(){
leftselected.clear();
for(String s : rightselected){
rightvalues.remove(s);
leftvalues.add(s);
}
system.debug('4444444444444444444444444'+leftvalues);
system.debug('5555555555555555555555555'+rightvalues);
return null;
}
// clone Records Based number of markets selecting(Ex:selecting c,d,e clones 3 records)
public PageReference updateMarkets(){
oppsToCreateList.clear();
selectedMarketsList.clear();
selectedMarketsList.addall(rightvalues);
system.debug('000000000000000000000'+rightvalues+selectedMarketsList);
for (Integer i=0; i<selectedMarketsList.size(); i++){
String themkt = selectedMarketsList.get(i);
boolean alreadyAdded = false;
//## see if the Market is already in the oppsToCreateList
for (Opportunity tmpopp : oppsToCreateList)
{
if (themkt == tmpopp.opportunity_multi__c)
{
alreadyAdded = true;
}
}
if (themkt == opp.opportunity_multi__c || alreadyAdded)
{
//## don't re-add the original opp to the list, and don't
//## add the same record to the list multiple times.
}
else
{
Opportunity newopp = opp.clone(false);
Newopp.OrderNumber__c = themkt;
newopp.accountid=acc.id;
newopp.ownerId=UserInfo.getUserId();
newopp.stagename='1 - Prospect Evaluation';
newopp.closedate=date.today();
newopp.Name =acc.name;
oppsToCreateList.add(newopp);
}
}
system.debug('1111111111111111111111111111111111111'+oppsToCreateList);
return null;
}
public PageReference SaveRecords(){
if (oppsToCreateList != null && oppsToCreateList.size() > 0)
{
try
{
insert oppsToCreateList;
}
catch (Exception e)
{
ApexPages.addMessages(e);
return null;
}
}
return new PageReference('/'+acc.Id);
}
}
==============Test class=============================================
@isTest()
public class TestmultipleRecordsOnOpportunity{
static testMethod void myUnitTest_creat_multiple_Opportunity()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c ='mp';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptions=myPageCon.getSelectedValues();
mypagecon.unselectclick();
List<SelectOption> testoptions11=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptionss=myPageCon.getSelectedValues();
list<String> originalvalues = new list<String>{'A','B','C'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_creat_multiple_Opportunity1()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='medium';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='High';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.selectedMarketsList =originalvalues ;
myPageCon.updateMarkets();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail1()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='low';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+null);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
//myPageCon.selectedMarketsList= 'UK;France';
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
}
and multipleRecordsOnOpportunity.SaveRecords()
from your unit test?
i a writing that class is standard controller Account an d extension name is multipleRecordsOnOpportunity.