function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JameslongJameslong 

NEED HELP IN WRITING A TEST CLASS...URGENT....PLEASE

Hi,

 

I am trying to write a test class for the below web service class....

 

Any help would be highly appreciated as I don't have enough knowledge on writing test classes...

 

CLASS:

 

global class AwebservClass {
public static final list<id> pricebookidlist= new list<ID>();

public static final list<String> productlist=new list<string>();

public static list<opportunitylineitem> olilist=new list<opportunitylineitem>();

global class result{

webservice list<list<oviidresultmap>> ovimapping=new list<list<oviidresultmap>>();

}
global class oviidresultmap{
webservice id Lineitemid;
webservice list<id> oviids= new list<id>();
webservice double LocalMRS;  //number 
webservice double LocalNRS;  //number 
webservice double MRS;    //number 
webservice double NRS;    //number 
webservice id localcurrency; //look up to custom object exchange rates

}
Webservice static result getOpplineitems(id oppid){

oviidresultmap ovii=new oviidresultmap();
map<id, map<id,list<id>>> OVIIds=new Map<id, map<id,list<id>>>();
result res=new result();
Map<id,list<id>> resultsmap=new Map<id,list<id>>();
res.ovimapping.add(getovimappedids(oppid));
system.debug('result'+res);
return res;
}

public static oviidresultmap[] getOVIMappedids(id oppid){
list<oviidresultmap> ress=new list<oviidresultmap>();
map<id,list<id>> p2OlmidMap=new map<id,list<id>>();
map<id,list<list<id>>> OVImap=new map<id,list<list<id>>>();

list<id> testid=new list<id>();
map<id,id> p2Olmidmap1= new map<id,id>();
olilist =[select id,product_family_pr4__c,pricebookentryid,Local_Currency__c,Local_MRS__c,Local_NRS__c,MRC__c,NRC__c from Opportunitylineitem where opportunityid =:oppid];
if(olilist.size()>0){
for(OpportunitylineItem olm:olilist){
pricebookidlist.add(olm.pricebookentryid);
productlist.add(olm.product_family_pr4__c);

}
system.debug('productnames'+productlist);

Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>(
[select product2.id from pricebookentry
where id in :pricebookidlist]);

for(OpportunitylineItem ollm:olilist){
oviidresultmap resss=new oviidresultmap();
resss.LocalMRS=ollm.Local_MRS__c;
resss.LocalNRS=ollm.Local_NRS__c;
resss.MRS=ollm.MRC__c;
resss.NRS=ollm.NRC__c;
resss.localcurrency=ollm.Local_Currency__c;
id test=entries.get(ollm.pricebookentryid).product2.id;
resss.oviids.addall(productids(test));
system.debug('finalll'+resss.oviids);
testid.add(ollm.id);
p2OlmidMap1.put(test,ollm.id);
system.debug('oviproduct'+p2OlmidMap1);
resss.Lineitemid=p2OlmidMap1.get(test);

ress.add(resss);
}

system.debug('fff'+ress);

}
return ress;
}
public static list<id> productids(id productid){
list<id> idlist=new list<id>();
for(OVI_value__c ovi:[select id,Product__c from OVI_value__c where Product__c=:productid]){
idlist.add(ovi.id);
system.debug('ss'+idlist);

}
return idlist;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

My Friend, I didn't see any logic in your test class to cover usecases of your webservice class-"AwebservClass ".

From the test class after inserting each item list you need to call the functions/methods of the webservice class to cover your use cases.

So, just call each fucntion by name of your class like this way-

AwebservClass.result res=AwebservClass.result.getOpplineitems(oppid);

 

in the same way you need to callup your each function.

All Answers

IspitaIspita

Hi,

Do you need pointers for covering the test method or you are expecting that someone actually provides you the actual test method?

 

 

UVUV

Go through link below and try out to write a test class.If you face any issue then post your questions.

http://tquilamockingbird.wordpress.com/2011/11/17/salesforce-testing-best-practices/

JameslongJameslong

Hi buddies,

 

In fact the actual test method would be highly appreciated if someone can provide me with.

 

and I have read in a few blogs that test class cannot be written to a web services class. I am confused...please clarify this.. 

 

Thanks for the link Umesh...that will get me started though but still looking for help.

 

Thanks

JL

UVUV

Dude,

 

You can write a test class for the webservice also..Its basically a kind of class to be used as a webservice..

As you develop test class for the apex classes, in the same way go ahead and develop for the webservice..Start writing and if you face any issue then we would help you out with the approach and code.

JameslongJameslong

Hi Umesh,

 

I have written code until this and it covers only 5% as i have included an instance only.... Please let me know how to proceed further to cover the code...

 

@istest
public class testAnOVIIDClass
{
static TestMethod void testAnOVIIDClass(){
//data preparation
list<opportunitylineitem> olilist=new list<opportunitylineitem>();
list<opportunity> opplist=new list<opportunity>();
list<id> pricebookidlist= new list<ID>();
list<String> productlist=new list<string>();
list<ovi_value__c> ovclist = new list<ovi_value__c>();
for(integer i=0;i<2;i++){
opportunity opy = new opportunity(Name = 'test opp' + i,stagename = 'closed won',closedate=date.parse('1/1/2020'));
opplist.add(opy);
}
/***for(integer i=0;i<2;i++){
opportunitylineitem aol= new opportunitylineitem(Name = 'test opp line item' + i);
olilist.add(aol);
}
***/
for(integer j=0;j<2;j++){
ovi_value__c ovv = new ovi_value__c(Name = 'test ovi value' + j);
ovclist.add(ovv);
}
//test.startTest();
insert opplist;
insert ovclist;
//test.stopTest();

list<opportunity> insertedopplist = new list<opportunity>([SELECT Name,Id,stagename FROM opportunity WHERE Id IN: opplist]);
for(opportunity opy : insertedopplist){
System.assertEquals(insertedopplist.size(),2);

list<ovi_value__c> insertedovclist = new list<ovi_value__c>([SELECT Name,ID FROM ovi_value__c WHERE Id IN: ovclist]);
for(ovi_value__c ovv : insertedovclist){
System.assertEquals(insertedovclist.size(),2);
}
// We're testing an instance method, so we need an instance
AnOVIIDClass anovi = new AnOVIIDClass();

 

}
}
}

 

Thanks

JL

UVUV

My Friend, I didn't see any logic in your test class to cover usecases of your webservice class-"AwebservClass ".

From the test class after inserting each item list you need to call the functions/methods of the webservice class to cover your use cases.

So, just call each fucntion by name of your class like this way-

AwebservClass.result res=AwebservClass.result.getOpplineitems(oppid);

 

in the same way you need to callup your each function.

This was selected as the best answer
JameslongJameslong

Hi Umesh,

 

Thanks a ton.... It worked magnificiently. I got 100% code coverage. 

 

Thanks

JL