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
Stephanie GethersStephanie Gethers 

deployment apex and visualforce and testing Apex.

Hello this is the first time that i used apex, so i build apex class and visualforce when i try to deploy it , i get error that i need to build apex test.. 
my apex class use only 3d api that i get ,and take all the data ..

Hello i am trying to upload apex class and visualForce and i get the error :
Your organization's code coverage is 0%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
Sum_Account_Revenue
No_Answer_Count
CountTaskNumber

some one tell me that i need to build testing Apex. but in the error i get files that alredy works few month :  Sum_Account_Revenue,No_Answer_Count,CountTaskNumber

 

so i dont know what to do , and if i need to do testing how i do it becouse all the documents apoke about triger and i dont have (i dont need )

public with sharing class appspotApiMedSuppController {
    public  string fieldName  {get;set;}
    public  JSONParser parser  {get;set;}
    
    public  string full_name {get;set;}
    public  string plan {get;set;}
    
    public  Integer quarter{get;set;}
    public  Integer annual{get;set;}
    public  Integer semi_annual{get;set;}
    public  Integer month{get;set;}
    public  rateClass rate{get;set;}
    
    public class rateClass {
        public  Integer quarter{get;set;}
        public  Integer annual{get;set;}
        public  Integer semi_annual{get;set;}
        public  Integer month{get;set;}
    }
    
    public  String type{get;set;}
    public  String name{get;set;}
    public  Double value{get;set;}
    
    public discountsClass discounts{get;set;}
    
    public class discountsClass {
        public  String type{get;set;}
        public  String name{get;set;}
        public  Double value{get;set;}
    }

    
    public class MedSupp {
       public  String full_name{get;set;}
       public  String plan{get;set;}
       public  String expires_date{get;set;}
       public  rateClass rate{get;set;}
       public  discountsClass  discounts{get;set;}
    }
    public MedSupp med_supp{get;set;}
    public List<MedSupp> dataList {get;set;}
   
    public String zip {get;set;}
    public String gender {get;set;}
    public String tobacco {get;set;}
    public String expires_date{get;set;}
    
    public String Dateeffective {get;set;}
    public String requestEndPoint {get;set;}
    public Date birth {get;set;}
    public Integer age {get;set;}
   
    public  appspotApiMedSuppController (ApexPages.StandardController stdController ){
        Date effectiveDate = Date.today();
        birth =  date.parse(System.currentPageReference().getParameters().get('birth'));
        
        zip = System.currentPageReference().getParameters().get('zip');
        gender = System.currentPageReference().getParameters().get('gender');
        tobacco = System.currentPageReference().getParameters().get('tobacco');
        
        String requestEndPoint='https://google.com';
        requestEndPoint= 'https://google.com?age='+age+'&zip5='+zip+'&gender='+gender+'&tobacco='+tobacco+'&effective_date='+Dateeffective;
        
       
        Http http =new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('x-api-token', '6a3e8d00b0183e4d9fd9a2dbd058ec26aecd412920034145254d780b0d6c2fe6');
        request.setEndpoint(requestEndPoint);
        request.setTimeout(15000);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        if(response.getStatusCode() ==200){
           dataList = new List<MedSupp>();
           
           JSONParser parser = JSON.createParser(response.getBody());
           while (parser.nextToken() != null) {
               
                if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                    while (parser.nextToken() != null) {
                         med_supp = new MedSupp();
                        fieldName = parser.getText();
                       
                        if (fieldName == 'company_base'){
                            
                            parser.nextToken();
                            if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                                while (parser.nextToken() != null)
                                {
                                    fieldName = parser.getText();
                                    if(fieldName == 'parent_company_base')
                                    {
                                        parser.nextToken();parser.nextToken();parser.nextToken();
                                        parser.nextToken();parser.nextToken();parser.nextToken();parser.nextToken();
                                        full_name = parser.getText();
                                        break;
                                    } 
                               
                                }
                            }
                        }
                        if (fieldName == 'rate'){
                            parser.nextToken();
                            rate =(rateClass)parser.readValueAs(rateClass.class);
                           
                        }
                        if (fieldName == 'plan'){
                            parser.nextToken();
                            plan = parser.getText();
                           
                           
                        }
                        if (fieldName == 'discounts'){
                            parser.nextToken();
                            parser.nextToken();
                            discounts=(discountsClass)parser.readValueAs(discountsClass.class);
                           
                        }
                         if (fieldName == 'location_base'){
                            parser.nextToken();
                            parser.nextToken();
                            parser.nextToken();
                            expires_date = parser.getText();
                            expires_date = expires_date.substring(0,expires_date.indexOf('T'));
                            
                        }
                        
                        if (fieldName == 'age'){
                          if((plan=='F') || (plan=='N')|| (plan=='G'))
                          {
                               med_supp.full_name = full_name;
                               med_supp.plan = plan;
                               med_supp.expires_date=expires_date;
                               med_supp.rate = rate;
                               med_supp.discounts = discounts;
                               dataList.add(med_supp);
                          }
                          
                        }
                      
                    }
                    
                }
                 
              
            }
        }
        else{
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,' There was an error retrieving the data information.');
        }
    }
}
Amit Singh 1Amit Singh 1
Hello,
To create test case for this you need to create Mock class which will be used for making the fake response and the test class.
In mock class you need to set the response as fake response. I noticed that JSON is the return type of your API callout. Use your actual response in the Mock Class so that you can cover the code.
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        String responseString = 'Your JSON String Here';
        // and method.
        System.assertEquals('Your End Point Here...', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(responseString);
        res.setStatusCode(200);
        return res;
    }
}
You can use below Link for generating the JSON as String.
https://www.adminbooster.com/tool/json2apex
Use below Link for reference.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Hope this helps :)

Thanks!
Amit Singh
Stephanie GethersStephanie Gethers

Hello i build it but this only 4 % and i dont know how to continue 

I builded file that 

@isTest global class MockHttpResponseGenerator implements HttpCalloutMock {
  // Implement this interface method

    global HTTPResponse respond(HTTPRequest req) {

        String responseString = '[{"company_base": {"business_type": "Life, Accident, and Health","established_year": 1998,"med_supp_state_market_data": [{"market_share": 0.344453599877,"state": "WY","lives": 14916,"premiums": 30178554,"claims": 24281001}],"underwriting_data": [],"med_supp_national_market_data": {"market_share": 0.315510079562,"lives": 3723184,"premiums": 8276072271,"claims": 6436017316},"established_year": 1998,"code": "707","name": "UNITEDHEALTH GRP","key": "aghzfmNzZ2FwaXIaCxINUGFyZW50Q29tcGFueRiAgICAoJKfCgyiAQhtZWRfc3VwcA","last_modified": "2016-11-11T16:42:52.240940"},"sp_rating": "AA-","naic": "79413","type": "STOCK",},"has_pdf_app": false,"rate": {"quarter": 42681,"annual": 168324,"semi_annual": 85362,"month": 14027},"rating_class": "Standard","fees": [],"rate_increases": [{"rate_increase": 0.0491573034,"date": "2013-04-01T00:00:00"}],"archive": null,"select": false,"age_increases": [0.0434875597,0.0416752067,0.0399422837,0.0384712412],"rate_type": "community rated","note": null,"discounts": [{"type": "percent","name": "household","value": 0.05}],"location": "aghzfmNzZ2FwaXIVCxIITG9jYXRpb24YgICA2vu9hAsMogEIbWVkX3N1cHA","legacy_id": null,"effective_date": "2016-04-01T00:00:00","company": "aghzfmNzZ2FwaXIUCxIHQ29tcGFueRiAgICAgO7UCAyiAQhtZWRfc3VwcA","e_app_link": "","location_base": {"expires_date": "2017-03-31T00:00:00","county": [],"zip3": [],"state": "PA","last_modified": "2017-02-08T21:58:48.929490","key": "aghzfmNzZ2FwaXIVCxIITG9jYXRpb24YgICA2vu9hAsMogEIbWVkX3N1cHA","effective_date": "2016-04-01T00:00:00","zip5": ["15001","19612"]},"last_modified": "2017-02-08T21:59:00.189200","plan": "F","key": "aghzfmNzZ2FwaXISCxIFUXVvdGUYgICA2tzP8wkMogEIbWVkX3N1cHA","riders": [],"expires_date": "2017-03-31T00:00:00","tobacco": false,"has_brochure": false,"gender": "M","age": 65}]';

        // and method.

        System.assertEquals('link that i insert', req.getEndpoint());

        System.assertEquals('GET', req.getMethod());


        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(responseString);
        res.setStatusCode(200);
        return res;
    }
}

and file that call it and more 
@isTest private class appspotApiMedSuppControllerTest{
    @isTest static void testCallOut(){
        Test.setMock(HttpCalloutMock.class ,new MockHttpResponseGenerator());
        HttpResponse response = appspotApiMedSuppController.getInfoFromExternalService();
        String valueResult = response.getBody();
        String expectString = '[{"company_base": {"business_type": "Life, Accident, and Health","established_year": 1998,"med_supp_state_market_data": [{"market_share": 0.344453599877,"state": "WY","lives": 14916,"premiums": 30178554,"claims": 24281001}],"underwriting_data": [],"med_supp_national_market_data": {"market_share": 0.315510079562,"lives": 3723184,"premiums": 8276072271,"claims": 6436017316},"established_year": 1998,"code": "707","name": "UNITEDHEALTH GRP","key": "aghzfmNzZ2FwaXIaCxINUGFyZW50Q29tcGFueRiAgICAoJKfCgyiAQhtZWRfc3VwcA","last_modified": "2016-11-11T16:42:52.240940"},"sp_rating": "AA-","naic": "79413","type": "STOCK",},"has_pdf_app": false,"rate": {"quarter": 42681,"annual": 168324,"semi_annual": 85362,"month": 14027},"rating_class": "Standard","fees": [],"rate_increases": [{"rate_increase": 0.0491573034,"date": "2013-04-01T00:00:00"}],"archive": null,"select": false,"age_increases": [0.0434875597,0.0416752067,0.0399422837,0.0384712412],"rate_type": "community rated","note": null,"discounts": [{"type": "percent","name": "household","value": 0.05}],"location": "aghzfmNzZ2FwaXIVCxIITG9jYXRpb24YgICA2vu9hAsMogEIbWVkX3N1cHA","legacy_id": null,"effective_date": "2016-04-01T00:00:00","company": "aghzfmNzZ2FwaXIUCxIHQ29tcGFueRiAgICAgO7UCAyiAQhtZWRfc3VwcA","e_app_link": "","location_base": {"expires_date": "2017-03-31T00:00:00","county": [],"zip3": [],"state": "PA","last_modified": "2017-02-08T21:58:48.929490","key": "aghzfmNzZ2FwaXIVCxIITG9jYXRpb24YgICA2vu9hAsMogEIbWVkX3N1cHA","effective_date": "2016-04-01T00:00:00","zip5": ["15001","19612"]},"last_modified": "2017-02-08T21:59:00.189200","plan": "F","key": "aghzfmNzZ2FwaXISCxIFUXVvdGUYgICA2tzP8wkMogEIbWVkX3N1cHA","riders": [],"expires_date": "2017-03-31T00:00:00","tobacco": false,"has_brochure": false,"gender": "M","age": 65}]';
        String contentType = response.getHeader('Content-Type');
        System.assertEquals(200,response.getStatusCode());
        System.assertEquals(valueResult,expectString);
        String fieldName,plan;
    }
    
    @isTest static void testClass(){
       String plan ='F';
       System.assertEquals('F',plan);
    }
}