You need to sign in to do that
Don't have an account?
Michael Hedrick 2
Test class issues with call out
Hello All,
I have an issue trying to figure out the Test classes for this code. I know I need a test class for this controller and a Mock Call Out class but I have been gettign continous errors in my test classes. Any help would be appreciated.
Regards,
M.
public with sharing class JdeOrderController {
Account account;
List<JdeOrderHeader> jdeData;
string message;
public JdeOrderController () {
message = '';
account = [SELECT stuff FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
if(String.isEmpty(account.stuff) || !account.JDE__c.isNumeric()) {
message = 'BLA BLA BLA' ;
}
else {
jdeData = RetrieveJdeData();
}
}
public string getMessage() {
return message;
}
public List<JdeOrderHeader> getJdeData() {
return jdeData;
}
private List<JdeOrderHeader> RetrieveJdeData() {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
JdeIntegration jde = new JdeIntegration();
String url = jde.buildUrl(account, 'order');
if (url == '') {
return null;
}
req.setEndpoint(url);
req.setMethod('GET');
res = http.send(req);
if(res.getstatusCode() == 200 && res.getbody() != null) {
List<JdeOrderHeader> jdeDataList = (List<JdeOrderHeader>)json.deserialize(res.getbody(), List<JdeOrderHeader>.class);
return jdeDataList;
}
return null;
}
}
I have an issue trying to figure out the Test classes for this code. I know I need a test class for this controller and a Mock Call Out class but I have been gettign continous errors in my test classes. Any help would be appreciated.
Regards,
M.
public with sharing class JdeOrderController {
Account account;
List<JdeOrderHeader> jdeData;
string message;
public JdeOrderController () {
message = '';
account = [SELECT stuff FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
if(String.isEmpty(account.stuff) || !account.JDE__c.isNumeric()) {
message = 'BLA BLA BLA' ;
}
else {
jdeData = RetrieveJdeData();
}
}
public string getMessage() {
return message;
}
public List<JdeOrderHeader> getJdeData() {
return jdeData;
}
private List<JdeOrderHeader> RetrieveJdeData() {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
JdeIntegration jde = new JdeIntegration();
String url = jde.buildUrl(account, 'order');
if (url == '') {
return null;
}
req.setEndpoint(url);
req.setMethod('GET');
res = http.send(req);
if(res.getstatusCode() == 200 && res.getbody() != null) {
List<JdeOrderHeader> jdeDataList = (List<JdeOrderHeader>)json.deserialize(res.getbody(), List<JdeOrderHeader>.class);
return jdeDataList;
}
return null;
}
}
Mock Call Out
This is, how i approach to this solution, if you come to know how to solve it better do post the test code that you tried implementing sot that we can move forward!
Reference to HttpMockCallOut (https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html)
All Answers
Mock Call Out
This is, how i approach to this solution, if you come to know how to solve it better do post the test code that you tried implementing sot that we can move forward!
Reference to HttpMockCallOut (https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html)
The Test Class code saved successfully but in the Developer console it fails.
"Methods defined as TestMethod do not support Web service callouts"
In the second test Class I had to remove this line of code "System.assertEquals(jdeCon.jdeData,'{"foo":"bar"}');" because it gave an error upon save
But I am assuming I need the Test_JDEIntegration2 class since this Test Class makes the Mock call? Do I need both
But if I try running it in the Developer Console it gives the following error:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Stack Trace:
Class.JdeOrderController.RetrieveJdeData: line 40, column 1
Class.JdeOrderController.<init>: line 14, column 1
Class.Test_JDEIntegration2.TestJDECOnnection: line 18, column 1
Any additional help will be appreiated
Regards,
M
I tried the first suggestion of using @isTest(SeeAllData=true) which I know is not considered best practive but I wanted to just tryy to get a successful test performed.
So this time when I run a Test I get the following errro:
System.AssertException: Assertion Failed: Expected: http://api.salesforce.com/foo/bar, Actual: https://mycutomurl/?param=f5q76QGBTbk1GJX7sHQgsknxOfqm7zMpOFmYdIefNI4%2FP05xJKENQBdnPyhQNyT%2FFeG3KvwN8gh%2F18h%2BNwSjNA%3D%3D
I think part of this issue is that I am encrypting the url in my Controller by calling another Class,
Or is this still related to the SQOL Query in my Controller?
Thanks,
M
Thank you!
I commented out the following 2 lines of code in the Mock Call Out Class.
Now when I run the Test I get the following error:
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Stack Trace:
Class.System.JSON.deserialize: line 15, column 1
Class.JdeOrderController.RetrieveJdeData: line 43, column 1
Class.JdeOrderController.<init>: line 14, column 1
Class.Test_JDEIntegration2.TestJDECOnnection: line 18, column 1
Something new evertime....
Thanks for your continued help Akhilesh.
Regards,
Michael
thank you!
I do not think I can edit the original post so I will add the additional classes below.
The Controller is listed in the Original Post.
I would rather not post the code for the 'JdeIntegration' Class. This is the Class that encrypts the URL to the remote server.
JDEWebServiceTest2 is the visualforce page. I did not think you needed this code but let me know if you do and I will post it.
These first 2 Classes are for defining the Data Structure
Controller Test Class
Mock Call Out
Pleas elet me know if you need me to Post any additional code.
Regards,
M
In the url you referenced I was looking at the last post on the page and was wondering do you know how they are defining the variable 'apxCls'?
Also, this edit is will be in teh Mock Call out Class?
Thanks,
M
for example in the below code you can see that the response has normalizedAddresses which the deserialize method dont like so they suggested to remove that from the link using String replace methods (https://developer.salesforce.com/forums/?id=906F00000008v5QIAQ) after you get the response from the callout! They are defining the apxCls same as your jdeDataList. this was in your controller!
myserverserver + '/?param=' + EncodingUtil.urlEncode(param, 'UTF-8');
param is made up of the Account.Id and JDE__c value.
Does that help any? I will look again at your previous post.
Thanks,
M
private List<JdeOrderHeader> RetrieveJdeData() {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
JdeIntegration jde = new JdeIntegration();
String url = jde.buildUrl(account, 'order');
if (url == '') {
return null;
}
req.setEndpoint(url);
req.setMethod('GET');
res = http.send(req);
// the response you get for this request is in JSON format, this format was not able to deserialize in the below code try to see this response
in the debug log and figur out!
System.debug('JSON Response of the call out'+res);
if(res.getstatusCode() == 200 && res.getbody() != null) {
List<JdeOrderHeader> jdeDataList = (List<JdeOrderHeader>)json.deserialize(res.getbody(), List<JdeOrderHeader>.class);
return jdeDataList;
}
return null;
}
}
I was finally able to successfully test my classes without errors. That was a good but painful exercise.
So now the question is, how can I insert conditions into my test class to cover situations like where the JDE__c value is vlaid and when its Null.
Can that be done in a single Test Class.
Regards,
M
If my solutions helped you to reach your goal, please choose this one as best answer.
Thank you!