Skip to main content
ofek Gold 님이 #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.');

}

}

}

답변 2개
  1. 2017년 2월 23일 오후 4:20
    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