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
Walter ZielenskiWalter Zielenski 

The Apex test class 'ParkLocatorTest' does not appear to be using the ParkServiceMock class correctly.

Hi all,

I am getting an error that I don't see addressed in the forum.
My tests pass with 100% code coverage when I run the test myself.
My class (from the WSDL) is named "public class ParkService"

Here are the classes I wrote for the challenge:
public class ParkLocator {
    public static string[] country(string theCountry) {
        ParkService.ParksImplPort  parkSvc = new  ParkService.ParksImplPort(); // remove space
        return parkSvc.byCountry(theCountry);
    }
}
@isTest
private class ParkLocatorTest {
    @isTest static void testCallout() {              
        Test.setMock(WebServiceMock.class, new ParkServiceMock ());
        String country = 'United States';
        List<String> result = ParkLocator.country(country);
        List<String> parks = new List<String>{'Yellowstone', 'Mackinac National Park', 'Yosemite'};
         System.assertEquals(parks, result); 
    }
}
@isTest
global class ParkServiceMock implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
        // start - specify the response you want to send
        ParkService.byCountryResponse response_x = new ParkService.byCountryResponse();
        response_x.return_x = new List<String>{'Yellowstone', 'Mackinac National Park', 'Yosemite'};
        // end
        response.put('response_x', response_x); 
   }
}
Please tell me what I am missing here.
Thanks,
Walter



 
Best Answer chosen by Walter Zielenski
Walter ZielenskiWalter Zielenski
Thanks Akhil,

Comparing our code, I found that there was a space in a line from my ParkLocatorTest class that was causing the challenge to fail.
Test.setMock(WebServiceMock.class, new ParkServiceMock ());
I removed the space after ParkServiceMock and before the ( and now I pass the challenge.

The space got added probably when I double clicked the phrase ParkServiceMock to copy and paste it.
Sometimes an extra space gets embedded in the copied string.

i guess you got to think like the guy who has to write the challenge validation code sometimes to get that badge.

Walter

All Answers

Akhil AnilAkhil Anil
Hi Walter,

All your code looks good to me. I had implemented something very similar and had passed the challenge.
 
public class ParkLocator {
    public static String[] country(String ctry) {
        ParkService.ParksImplPort prk = 
            new ParkService.ParksImplPort();
        return prk.byCountry(ctry);
    }
}
 
@isTest
global class ParkServiceMock implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
        // start - specify the response you want to send
        ParkService.byCountryResponse response_x = 
            new ParkService.byCountryResponse();
            
        List<String> myStrings = new List<String> {'Park1','Park2','Park3'};
    
        response_x.return_x = myStrings;
        // end
        response.put('response_x', response_x); 
   }
}
 
@isTest
private class ParkLocatorTest  {
    @isTest static void testCallout() {              
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new ParkServiceMock());
        // Call the method that invokes a callout
        List<String> result = new List<String>();
        List<String> expectedvalue = new List<String>{'Park1','Park2','Park3'};
        
        result = ParkLocator.country('India');
        // Verify that a fake result is returned
        System.assertEquals(expectedvalue, result); 
    }
}

Hope that helps !
Walter ZielenskiWalter Zielenski
Thanks Akhil,

Comparing our code, I found that there was a space in a line from my ParkLocatorTest class that was causing the challenge to fail.
Test.setMock(WebServiceMock.class, new ParkServiceMock ());
I removed the space after ParkServiceMock and before the ( and now I pass the challenge.

The space got added probably when I double clicked the phrase ParkServiceMock to copy and paste it.
Sometimes an extra space gets embedded in the copied string.

i guess you got to think like the guy who has to write the challenge validation code sometimes to get that badge.

Walter
This was selected as the best answer
Akhil AnilAkhil Anil
Hey Walter,

That was a good catch !

Kindly mark it as an answer so that we can close this thread.
Nidhi gupta 150Nidhi gupta 150

Corrected Codes! Upvote and close if this helps!!
 

ParkService Class generated from WSDL:

//Generated by wsdl2apex

public class ParkService {
    public class byCountryResponse {
        public String[] return_x;
        private String[] return_x_type_info = new String[]{'return','http://parks.services/',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class byCountry {
        public String arg0;
        private String[] arg0_type_info = new String[]{'arg0','http://parks.services/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://parks.services/','false','false'};
        private String[] field_order_type_info = new String[]{'arg0'};
    }
    public class ParksImplPort {
        public String endpoint_x = 'https://th-apex-soap-service.herokuapp.com/service/parks';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://parks.services/', 'ParkService'};
        public String[] byCountry(String arg0) {
            ParkService.byCountry request_x = new ParkService.byCountry();
            request_x.arg0 = arg0;
            ParkService.byCountryResponse response_x;
            Map<String, ParkService.byCountryResponse> response_map_x = new Map<String, ParkService.byCountryResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://parks.services/',
              'byCountry',
              'http://parks.services/',
              'byCountryResponse',
              'ParkService.byCountryResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}

ParkLocator Class:


public class ParkLocator {
    public static String[] country(String country){
        ParkService.ParksImplPort Locator = new ParkService.ParksImplPort();
        return Locator.byCountry(country);
    }
}


ParkServiceMock class:

@isTest
global class ParkServiceMock implements WebServiceMock{
    global void doInvoke(
    Object stub,
    Object request,
    Map<String,Object> response,
    String endpoint,
    String soapAction,
    String requestName,
    String responseNS,
    String responseName,
        String responseType) {
            ParkService.byCountryResponse response_x = new ParkService.byCountryResponse();
            response_x.return_x = new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'};
                response.put('response_x',response_x);
        }

}
ParkLocatorTest class:

@isTest
private class ParkLocatorTest {
    testMethod static void testCallout(){
         Test.setMock(WebServiceMock.class, new ParkServiceMock());
         String country = 'United States';
         String[] result = ParkLocator.country(country);
        System.assertEquals(new List<String>{'Garner State Park', 'Fowler Park', 'Hoosier National Forest Park'}, result);
    }
}

upvote to help others