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
Pedro Garcia 32Pedro Garcia 32 

Unit test with nested methods

Hi,

I'm creating a Unit Test for a method that calls to another callout method.

I got the following error: Methods defined as TestMethod do not support Web service callouts

I've already created the Unit Test for the callout method and everything works OK.

How could I do it?

cass a{

public mycalloutmethod(url){ return Response.body}

public mymethodfortesting(){
String a = mycalloutmethod('myurl');
do something else
}
}

Thanks,
Pedro

 
Best Answer chosen by Pedro Garcia 32
Abdul KhatriAbdul Khatri
Try to work out through this
Here is the detail how to do
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

Please change the response you are expecting.
class A
{

	public static String getREST(String url)
	{ 
		//MAKE THE CONNECTION AND RETURNS THE result.body()
	}
	
	public static Boolean process(String A, String B)
	{

		String body = getREST(A+B);

		//process body and return a boolean
	}
}

@isTest 
class ATest{

	@isTest void static processTest()
	{

		Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator())

		System.assertEquals(true, A.process('http://mydomain.com','/service');
		//THE ERROR HERE IS: Methods defined as TestMethod do not support Web service callouts
	}

	private class MockHttpResponseGenerator implements HttpCalloutMock {
	    // Implement this interface method
	    private HTTPResponse respond(HTTPRequest req) {
	        // Optionally, only send a mock response for a specific endpoint
	        // and method.
	        System.assertEquals('http://example.com/example/test', req.getEndpoint());
	        System.assertEquals('GET', req.getMethod());
	        
	        // Create a fake response
	        HttpResponse res = new HttpResponse();
	        res.setHeader('Content-Type', 'application/json');
	        res.setBody('{"example":"test"}');
	        res.setStatusCode(200);
	        return res;
	    }
	}	
}

 

All Answers

Abdul KhatriAbdul Khatri
Can you clarify with some code here? at least I am not able to understand.
Pedro Garcia 32Pedro Garcia 32
class A{

public static String getREST(String url){ //MAKE THE CONNECTION AND RETURNS THE result.body()}

public static Boolean process(String A, String B){

String body = getREST(A+B);

//process body and return a boolean
}
}

@isTest class ATest{

@isTest void static processTest(){

System.assertEquals(true, A.process('http://mydomain.com','/service');
//THE ERROR HERE IS: Methods defined as TestMethod do not support Web service callouts
}
}

Hi Abdul, please, let me know if this is clearer for you.
Abdul KhatriAbdul Khatri
Try to work out through this
Here is the detail how to do
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

Please change the response you are expecting.
class A
{

	public static String getREST(String url)
	{ 
		//MAKE THE CONNECTION AND RETURNS THE result.body()
	}
	
	public static Boolean process(String A, String B)
	{

		String body = getREST(A+B);

		//process body and return a boolean
	}
}

@isTest 
class ATest{

	@isTest void static processTest()
	{

		Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator())

		System.assertEquals(true, A.process('http://mydomain.com','/service');
		//THE ERROR HERE IS: Methods defined as TestMethod do not support Web service callouts
	}

	private class MockHttpResponseGenerator implements HttpCalloutMock {
	    // Implement this interface method
	    private HTTPResponse respond(HTTPRequest req) {
	        // Optionally, only send a mock response for a specific endpoint
	        // and method.
	        System.assertEquals('http://example.com/example/test', req.getEndpoint());
	        System.assertEquals('GET', req.getMethod());
	        
	        // Create a fake response
	        HttpResponse res = new HttpResponse();
	        res.setHeader('Content-Type', 'application/json');
	        res.setBody('{"example":"test"}');
	        res.setStatusCode(200);
	        return res;
	    }
	}	
}

 
This was selected as the best answer
Pedro Garcia 32Pedro Garcia 32
Thanks... I'll test it and let you know
Pedro Garcia 32Pedro Garcia 32
Thanks... it works!!!!!