-4

I wrote one program to display the present dollar rate. this is my code,

public class Currency_Layer {
    public String result     {set;get;}

    public void getCurrency() {
        Http p =new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:Currency_Layer');
        request.setMethod('GET');
        HttpResponse response = p.send(request);
        result = response.getBody();
}
}

and my page is:

<apex:page controller="Currency_Layer">
    <apex:form >
        <apex:commandButton value="Currency" action="{!getCurrency}"/>
        {!result}
     </apex:form>
</apex:page>

Now please help me how to write the test class for the above code

  • 2
    While you've received some answers (two of them incorrect), "please write this code" requests are generally considered off-topic and poorly received on Salesforce Stack Exchange. – David Reed Apr 2 '18 at 12:50
0

Create one class with name MockHttpResponseGenerator which returns dummy response

@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global static HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint and method.

        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        System.assertEquals(res != null,true);
        return res;
    }
}

Edit your getCurrency method of controller class as below:

 public void getCurrency() {
        Http p =new Http();
        HttpResponse response;
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:Currency_Layer');
        request.setMethod('GET');
        if(!test.isRunningTest())
             response = p.send(request);
        else 
             response = MockHttpResponseGenerator.respond(req);    
        result = response.getBody();
}

And Write the test calss as below

@istest(seeAllData = false) 
public class Currency_LayerTest{
    @isTest
    public static void GetCurrencyTest(){

        test.startTest();
        Currency_Layer layerObj = new Currency_Layer (); 
        String res = layerObj.getCurrency();
        system.assertEquals(res != null, true);
        test.stopTest();
    }
}
| improve this answer | |
  • When you use HTTPCalloutMock you should be calling Test.setMock(), not checking for test context in production code. That defeats the purpose of the mock. Asserting res != null in respond() is unnecessary, and it would be better to do a more specific assert in GetCurrencyTest(). – David Reed Apr 2 '18 at 10:20
1

Although the answer by Shilpa is correct, it does not utilize the mock response generated by setting the mock response as the actual response.

Create a test class that sends a mock response

@isTest
global class MockHttpResponseTest implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {

        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"name":"value"}');//change the body to the response you need
        res.setStatusCode(202);//If you are checking statuscode in your code for success response
        return res;
    }
}

Use this mock response to mock the response on callout

@isTest
private class Currency_LayerTest{  
    static testMethod void testCallout() {
        // Set mock callout class 


        Test.setMock(HttpCalloutMock.class, new MockHttpResponseTest ());

        // Call method to test.
        // This causes a fake response to be sent from the class that   implements HttpCalloutMock. 
          Currency_Layer cL= new Currency_Layer();
          cL.getCurrency();
          System.assertEquals('{"name":"value"}', cL.result);
    }

Also please refer the below documentation for creating test classes for testing http callout: HTTP Callout by implementing httpCalloutMockInterface

| improve this answer | |
-2

A test class is a simple apex class but there is a slight difference between them.

  1. You need to use @isTest annotation before the declaration of your class.
  2. You will also need to specify @isTest annotation with your test class methods.

Both of these are explained below.

@isTest private class Currency_LayerTestClass{ @isTest static void getCurrencyTest() { // code_block Currency_Layer currencyLayerObj = new Currency_Layer(); currencyLayerObj.getCurrency(); } }

| improve this answer | |
  • thanks shubham for your reply, it's help me lot and the test code got 75% code coverage – srikanth Apr 2 '18 at 9:39
  • This is not a unit test; it is a smoke test. It performs no assertions and therefore demonstrates nothing about code behavior. Additionally, it fails to mock the callout being performed. Please see How To Write Good Unit Tests. – David Reed Apr 2 '18 at 10:19
  • You need to include Mock class for that to test your code. – Piyush Kumar Apr 2 '18 at 11:21
  • I just wanted to show a path to the requesting user.That is all.It helped him to proceed further.The purpose is served. – Shubham Prajapati Apr 3 '18 at 17:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.