You need to sign in to do that
Don't have an account?
buggs sfdc
Need help in covering Test class more 85%
Hi
I have class which i have to cover more than 80%,i tried to cover it but some how am seeing few errors! Any help Greatly Appreciated!
Here am attching my class and test class!
I have class which i have to cover more than 80%,i tried to cover it but some how am seeing few errors! Any help Greatly Appreciated!
Here am attching my class and test class!
public class Mailtemplate{ public static Map<String,Email_notification__c> EMN = Email_notification__c.getAll(); //Method to get all templates public static Map<String,String> method1(){ Map<String,String> Templates = new Map<String,String>(); String username = EMN.get('SIT').UserName__c; String password = EMN.get('SIT').Password__c; Boolean success; Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); HttpRequest req = new HttpRequest(); req.setHeader('Authorization', authorizationHeader); req.setHeader('Content-Type','application/json'); req.setEndpoint('https://api.EF.com/v3/templates'); req.setMethod('GET'); try{ //send request Http h = new Http(); HttpResponse res = h.send(req); System.debug('Responce : '+ res.getBody()); String responceBody = res.getBody(); System.debug('Response from EMailnote:' + responceBody); ControllerJson obj = new ControllerJson(); obj = (ControllerJson)ControllerJson.parseTemplate(responceBody); List<ControllerJson.mailtemp> lstObjTemp = (List<ControllerJson.mailtemp>) obj.templates; for(ControllerJson.mailtemp template : lstObjTemp) { System.debug('Template Id : '+ template.id +', Template Name : '+template.name); Templates.put(template.name,template.id); } //check response String status = res.getStatus(); if(status.toUpperCase()=='OK'){ success=true; } else{ success=false; } } catch(Exception e){ System.Debug(LoggingLevel.ERROR,'****** Template.'); success=false; } return Templates; } public static boolean method2(String toEmail){ string Recipient = toEmail; string CC; string Subject; string Body; boolean Success; CC = 'testmail@gmail.com'; subject = 'Test : Hello'; Body = 'Hi'; String requestBody=''; requestBody += 'to[]='+EncodingUtil.urlEncode(Recp.meth(),'UTF-8'); requestBody += '&to[]='+EncodingUtil.urlEncode('testmail@gmail.com','UTF-8'); requestBody += '&from='+EncodingUtil.urlEncode('testmail@gmail.com','UTF-8'); requestBody += '&fromname='+EncodingUtil.urlEncode('testmail','UTF-8'); requestBody += '&subject='+EncodingUtil.urlEncode(subject,'UTF-8'); requestBody += '&text='+EncodingUtil.urlEncode(Body.trim(),'UTF-8'); requestBody += '&html='+EncodingUtil.urlEncode(Body.trim(),'UTF-8'); requestBody += '&api_user='+EMN.get('SIT').UserName__c; requestBody += '&api_key='+EMN.get('SIT').Password__c; HttpRequest req = new HttpRequest(); req.setEndpoint('https://Edf.com/api/mail.send.json'); //req.setHeader('Content-Type','application/json'); req.setMethod('POST'); req.setBody(requestBody); try{ //send request Http h = new Http(); HttpResponse res = h.send(req); res.setHeader('Content-Type','application/json'); System.debug('Response from mailtemp:' + res); //check response String status = res.getStatus(); if(status.toUpperCase()=='OK'){ success=true; } else{ success=false; } } catch(Exception e){ System.Debug(LoggingLevel.ERROR,'****** email.'); success=false; } return success; } //Method to send Email using Template public static boolean method3(String str){ string Recipient; string RecipientName; string to; string Subject; string Body; boolean Success; String requestBody=''; system.debug('JsonString'+str); to = 'testmail12@gmail.com'; Body = 'sample mail '; //Text Mandatory subject = ' '; requestBody += 'to[]='+EncodingUtil.urlEncode(to.trim(),'UTF-8'); requestBody += '&x-smtpapi='+str; requestBody += '&from='+EncodingUtil.urlEncode('testmail@gmail.com','UTF-8'); requestBody += '&fromname='+EncodingUtil.urlEncode('sample temp','UTF-8'); requestBody += '&subject='+EncodingUtil.urlEncode(subject,'UTF-8'); requestBody += '&text='+EncodingUtil.urlEncode(Body.trim(),'UTF-8'); requestBody += '&html='+EncodingUtil.urlEncode(Body.trim(),'UTF-8'); requestBody += '&api_user='+EMN.get('SIT').UserName__c; requestBody += '&api_key='+EMN.get('SIT').Password__c; system.debug('-----------------'+ requestBody); HttpRequest req = new HttpRequest(); req.setEndpoint('https://mailtemp.com/api/mail.send.json'); //req.setHeader('Content-Type','application/json'); req.setMethod('POST'); req.setBody(requestBody); try{ //send request Http h = new Http(); HttpResponse res = h.send(req); res.setHeader('Content-Type','application/json'); System.debug('Response from mailtemp:' + res); //check response String status = res.getStatus(); if(status.toUpperCase()=='OK'){ success=true; } else{ success=false; } } catch(Exception e){ System.Debug(LoggingLevel.ERROR,'****** email.'); success=false; } return success; } public static String method4(String date1, String date2){ String requestBody=''; String responceBody; requestBody += 'start_date='+EncodingUtil.urlEncode(startDate,'UTF-8'); requestBody += '&end_date='+EncodingUtil.urlEncode(endDate,'UTF-8'); requestBody += '&api_user='+EMN.get('SIT').UserName__c; requestBody += '&api_key='+EMN.get('SIT').Password__c; HttpRequest req = new HttpRequest(); req.setEndpoint('https://api.mailtemp.com/api/bounces.get.json'); req.setMethod('GET'); req.setHeader('Content-Type','application/x-www-form-urlencoded'); req.setBody(requestBody); req.setHeader('Content-Length',String.valueof(requestBody.length())); try{ //send request Http h = new Http(); System.HttpResponse res = h.send(req); responceBody= res.getBody(); } catch(Exception e){ } return responceBody; } }
@isTest private with sharing class Test_Mailtemplate { //public class MockHttpResponseGenerator implements HttpCalloutMock{ static testMethod void Test_method1() { string testmail; Email_notification__c EMN= new Email_notification__cEmail_notification__c(); EMN.Name = 'test name'; EMN.Username__c = 'testuname'; EMN.Password__c = 'testpswd'; Insert EMN; Test.startTest(); Mailtemplate rc = new Mailtemplate(); Mailtemplate.method1(); Test.stopTest(); } static testMethod void Test_Method2() { string testmail; Test.startTest(); Mailtemplate.Method2(testmail); Test.stopTest(); } static testMethod void Test_Method3() { string testmail1; Test.startTest(); Mailtemplate.Method3(testmail1); Test.stopTest(); } static testMethod void Test_Method4() { string startdate; string enddate; Test.startTest(); Mailtemplate.Method4(startdate,enddate); Test.stopTest(); } static testmethod void testMe() { Test.setMock(HttpCalloutMock.class, new Stubby()); HttpRequest req = new HttpRequest(); req.setEndpoint('http://123.63.33.43/blank/sms/user/urlsmstemp.php?'); req.setMethod('GET'); Http h = new Http(); HttpResponse res = Mailtemplate.method1(); String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"foo":"bar"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200,res.getStatusCode()); } }
One thing I noticed was that you are not setting your test variables to any values - thus only passing in NULL to your methods. For example string testmail on line 24. You should probably set that to something you would actually expect to use the method.
I would suggest that you name your methods and variables better so you can tell what they are suppose to do based on name.
Thanks