+ 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;
    }
}