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
Ryan Adams 173Ryan Adams 173 

Generate an Apex class using WSDL2Apex and write a test class.

Please help me resolve this challenge:

https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_soap_callouts

The Challenge is as follows:

Generate an Apex class using WSDL2Apex and write a test class.
Generate an Apex class using WSDL2Apex for a SOAP web service, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.

Use WSDL2Apex to generate a class called 'ParkService' in public scope using this WSDL file. After you click the 'Parse WSDL' button don't forget to change the name of the Apex Class Name from 'parksServices' to 'ParkService'.
Create a class called 'ParkLocator' that has a 'country' method that uses the 'ParkService' class and returns an array of available park names for a particular country passed to the web service. Possible country names that can be passed to the web service include Germany, India, Japan and United States.
Create a test class named ParkLocatorTest that uses a mock class called ParkServiceMock to mock the callout response.
The unit tests must cover all lines of code included in the ParkLocator class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.

The error I receive when checking the challencge is:

Challenge Not yet complete... here's what's wrong:
Executing the 'country' method on 'ParkLocator' failed. Make sure the method exists with the name 'country', is public and static, accepts a String and returns an array of Strings from the web service.

Here is the code I am using:
public class ParkLocator {
    public static String[] country(String ctry) {
        ParkService.ParksImplPort prk = 
            new ParkService.ParksImplPort();
        return prk.byCountry(ctry);
    }
}

and
 
@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); 
   }
}

and
 
@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); 
    }
}

Any help which can be provided is greatly appreciated.  If you could advise me at raadams173@gmail.com if you reply with a solution, I can log in to check it.

Thanks.

Ryan
Best Answer chosen by Ryan Adams 173
Amit Singh 1Amit Singh 1
Hello Ryan,

Use below code for ParkLocator class.
public class ParkLocator {
    public static String[] country(String country){
        ParkService.ParksImplPort parks = new ParkService.ParksImplPort();
        String[] parksname = parks.byCountry(country);
        return parksname;
    }
}
If this not resolves the problem then use a new Developer Org for completing the Challenge.

Let me know if this helps :)

 

All Answers

LBKLBK
Everything seems to be in place, as expected.

I believe that you have changed the proxy class name from parksServices to ParkService and run the tests once before checking the challenge (because those are the only things not visible to me in the question above).

Can you Check the challenge once again and look for the log file in Developer Console?

This log file may show some path.
Amit Singh 1Amit Singh 1
Hello Ryan,

Use below code for ParkLocator class.
public class ParkLocator {
    public static String[] country(String country){
        ParkService.ParksImplPort parks = new ParkService.ParksImplPort();
        String[] parksname = parks.byCountry(country);
        return parksname;
    }
}
If this not resolves the problem then use a new Developer Org for completing the Challenge.

Let me know if this helps :)

 
This was selected as the best answer
Ryan Adams 173Ryan Adams 173
To all who replied = THANKS.

I kept trying offered solutions in other threads and eventually came across this one:

James Loghry
Joe, as others have seen, try removing the spaces between ParkLocator and the curly brace and the country method and curly brace, and change String[] to List<String> to see if that helps.
 
1public class ParkLocator{
2    public static List<String> country(String countryName){
3        ParkService.ParksImplPort park = new ParkService.ParksImplPort();
4        return (List<String>)park.byCountry(countryName);
5    }
6}

I changed my ParkLocater class making these corrections, reran the Challenge and was successful.

After completing the challenge, I went back and replaced the ParkLocater code with that provided by Amit above, and once again the challenge check ran correctly.  Kudos to Amit!

Once again, thanks.

Ryan
 
Navin NagraniNavin Nagrani
Hi All , 
I am facing the following error while trying to complete the challenge
Challenge not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.CalloutException: Web service callout failed: Unable to find Apex package for :parksServices

Can someone help?
Thanks,
Navin
Fede FraschiniFede Fraschini
I have updated from "parksServices" to "ParkService" on ParkService and AsyncParksServices classes. Also update the strings references.
Debaranjan GhoshDebaranjan Ghosh
Above Code helped me a lot to fix this problem. what I did was I took these 3 classes and saved each of them so that they are compiled sucessfly ( ie Problem tab should not throw any error) and next I clicked  Run Test button for the ParkLocatorTest class which displayed 100% code coverage! . I also did Test | Run All. as adviced in the lesson  and this helped me to successfully complete this Challenge.
 
rajasekhar tandasarajasekhar tandasa
Hi
i am getting following error  after saveing the below classs.
invalid api version 0.0.

public class ParkLocator {
    public static String[] country(String country){
        ParkService.ParksImplPort parks = new ParkService.ParksImplPort();
        String[] parksname = parks.byCountry(country);
        return parksname;
    }
}
Sumit Bhattacharya 10Sumit Bhattacharya 10
public class ParkLocator {
    public static String[] country(String country){
        ParkService.ParksImplPort parks = new ParkService.ParksImplPort();
        String[] parksname = parks.byCountry(country);
        return parksname;
    }
}

=====================================================================================================
1. Invalid type: ParkService.ParksImplPort
2. Variable does not exist: parks

i am getting following error ????
Vandana Kewlani 3Vandana Kewlani 3

Because you have not created ParkService class and therefore you cannot create its object
Thangamani Nachimuthu 7Thangamani Nachimuthu 7
Can anyone provide us the ParkService class?
Rupeshk67Rupeshk67
//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;
        }
    }
}
Prem KrishnanPrem Krishnan
@Sumit Bhattacharya 10
Invalid type: ParkService.ParksImplPort is coming may be because that class created from wsdl is not visible to the ParkLocator class. can you pls paste all the code like the wsdl class, and test classes?!
Manjiri Yatnalkar 1Manjiri Yatnalkar 1
The probable reason for the above-specified error is 'ParkService' class is not created properly. Delete the existing classes and again generate 'ParkService' with a proper name.
 
rahul amin roktimrahul amin roktim
i can do this using php language. in stackoverflow lot of answer avilable about ParkService
contact me (https://bestprimevideo.com/contact-us/)
Prashant Kumar 455Prashant Kumar 455
Thanks for sharing the answer to this question. I was searching for this from last one month.
Universe Tale (https://www.universetale.com/)
Learn Forget (https://learnforget.com/)
Praveen T 19Praveen T 19
Hi ,

  I am gettig below error. Can someonce guide.

Executing the 'country' method on 'ParkLocator' failed. Make sure the method exists with the name 'country', is public and static, accepts a String, and returns an array of Strings from the web service.
Ankana SadhukhanAnkana Sadhukhan
Go to Remote Site Settings and create a new remote site with the Remote Site URL as     https://th-apex-soap-service.herokuapp.com . Make it active and try to re-run the challenge, it will be completed.
vikrant Srivastavavikrant Srivastava
@Ankana Sadhukhan 
i have tried everything but only your solution gave me help thankyou so much
yogesh watileyogesh watile
This code working fine for me
public class ParkLocator {
    public static List<String> Country(String name) {
        List<String> countries = new List<String>();
         ParkService.ParksImplPort park = new ParkService.ParksImplPort();
        countries = park.byCountry(name);
        return countries;
    }
}



@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
        String[] parks = ParkLocator.Country('Japan');
        // Verify that a fake result is returned
        system.assert(parks.size() > 0);
    }
}



@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>{'Germany', 'India', 'Japan', 'United States'};
            response.put('response_x', response_x); 
   }
}