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
DDayDDay 

Help with test method

Hello,

 

I have the following method which gets some XML from a web service call, parses through the data and passes it on for display in a VF page. I have everything working as I need it to but don't know how write a test case for it. I've tried writing a test case that uses bogus data and compares that to this method but it still complains that every line is not being covered. Any help?

 

 

public class UserConference {
public String usr_conference_id { get; set; }
public String conference_name { get; set; }
}

public List<Conference.UserConference> getConferenceData(HttpResponse res) {
XMLDom responseXML = new XMLDom(res.getBody());

UserConference[] conference_list = new UserConference[0];
all_conferences = responseXML.getElementsByTagName(xml_search_tag);

for (Integer i=0; i < all_conferences.size(); i++) {
UserConference conference = new UserConference();

child_nodes = all_conferences.get(i).childNodes;
for (Integer c=0; c < child_nodes.size(); c++) {
conf_element = child_nodes.get(c);
if (conf_element.nodeName == 'usr_conference_id') conference.usr_conference_id = conf_element.textContent();
if (conf_element.nodeName == 'conference_name') conference.conference_name = conf_element.textContent();
conference_list.add(conference);
}
return conference_list;
}

 

Thanks,

Dan

 

 

wesnoltewesnolte

Hey

 

You have a number of 'if' conditions within the code and it's probably one or more of these lines that it's not getting too. The test should tell you which lines it doesn't reach.. using this you will have to alter your test data, or perhaps create more than one set of test data such that it travels through every branch. If you need more help perhaps post your test code and test data and we can check it out for you.

 

Cheers,

Wes 

DDayDDay

Hi Wes,

 

Thanks for the info. How do I go about testing the individual 'if' conditions? Here's is me test code so far. If I run it it sill says nearly every line in my method is not covered.

 

 

static testMethod void testConferenceData() { // BUILD UP TEST DATA Conference.UserConference[] conference_list = new Conference.UserConference[0]; Conference.Userconference conference = new Conference.Userconference(); conference.usr_conference_id = '25531'; conference.conference_name = 'asdfasdf'; conference_list.add(conference); // GET DATA FROM METHOD Conference this_conf = new Conference(); this_conf.xml_search_tag = 'conference'; this_conf.api_url = 'https://mysite.com/'; // SINCE I'M USING A WEB SERVICE I HAVE TO POPULATE THE HttpRequest BODY WITH SAMPLE DATA Http h = new Http(); HttpRequest req = this_conf.buildAPICall(); req.setBody('<conference><usr_conference_id>25531</usr_conference_id><conference_name>asdfasdf</conference_name>'); HttpResponse res = this_conf.callAPI(h, req); XMLDom responseXML = new XMLDom(res.getBody()); List<Conference.UserConference> test_method = this_conf.getConferenceData(res); // COMPARE THE VALUES system.assertEquals(conference_list.size(), test_method.size()); if (conference_list.size() == test_method.size()) { system.assertEquals(conference_list[0].usr_conference_id, test_method[0].usr_conference_id); system.assertEquals(conference_list[0].conference_name, test_method[0].conference_name); } }

 

 

 

wesnoltewesnolte

Hey

 

In your test class you set the  REQUEST body to this value

 

<conference><usr_conference_id>25531</usr_conference_id><conference_name>asdfasdf</conference_name>

 

But your method processes the RESPONSE 

 

this_conf.getConferenceData(res); 

 

Are you sure that the response contains the XML

 

<conference><usr_conference_id>25531</usr_conference_id><conference_name>asdfasdf</conference_name>

 

If it doesn't contain that XML structure you if-statements won't be processed. I would suggested putting a System.debug message after this line

 

XMLDom responseXML = new XMLDom(res.getBody());

System.debug(responseXML);

 

And check the output in the debug log. My guess is that the XML response your getting back isn't structured teh way you expect it.

 

Cheers,

Wes

 

wesnoltewesnolte

If you just need to test that method perhaps just pass it a reponse that you've made

 

e.g.

 

HttpResponse res = new HttpResponse();

res.setBody('<conference><usr_conference_id>25531</usr_conference_id><conference_name>asdfasdf</conference_name>');

List<Conference.UserConference> test_method = this_conf.getConferenceData(res);

 

Cheers,

Wes

DDayDDay

Looks like the tests aren't even getting that far. They weren't reporting any errors but looking in the debug log I found this:

 

*** Beginning Test 14: ConferenceTest.static testMethod void testConferenceData()

20090702171556.881:Class.Conference: line 435, column 17: SOQL query with 1 row finished in 2 ms
20090702171556.881:Class.ConferenceTest.testConferenceData: line 241, column 21: returning System.HttpRequest from method public System.HttpRequest buildAPICall() in 0 ms
System.TypeException: Testmethods do not support webservice callouts.

Class.Conference.callAPI: line 72, column 22
Class.ConferenceTest.testConferenceData: line 243, column 22

 

I followed the documentation in the "Intro to Apex Code Test Methods" guide and separated the HttpRequest and HttpResponse into separate methods:

 

 

// CREATE HTTP REQUEST TO CALL API
public HttpRequest buildAPICall() {
HttpRequest req = new HttpRequest();
req.setEndpoint(api_url);
req.setMethod('GET'); return req;
}

// MAKE API CALL
public HttpResponse callAPI (Http h, HttpRequest req) {
HttpResponse res = h.send(req);
return res;
}

 

Not sure how I should be building up this data in my test class to effectively test it.

 

Thanks,

Dan

 

 

 

DDayDDay

Ah, I see what you mean about setting the request body instead of the response. Is it possible to set the body of HttpResponse? I can't find any documentation on it other than some other unanswered posts.

 

Thanks,

Dan

wesnoltewesnolte

Oops. Yeah you can't make call out in test methods. I was wondering how you weren't getting any errors but thought that you were dealing with them somehow.

 

I found an interesting article here: http://www.perkiset.org/forum/index.php?action=printpage;topic=1489.0 that will help you get the code coverage you need.

 

Perhaps you should put the actual parsing of the of the XML into a seperate method and test that on it's own too. That way you will know that area is tested. The testing methodologies when it comes to making outbound calls is to isolate the call as much as possible because you can't test that actual line of code. 

 

Some more details can be found here: http://wiki.developerforce.com/index.php?title=Virtual_Callout_Testing

 

Hope that helps

Wes