Integrating with the Service

We have one unfinished piece of work in the Airport class—the fetchData() function is pending implementation. We need to replace the exception in that function with code to talk to the web service. Since it needs a network connection to talk to an external service, the test for fetchData() will be an integration test and not a unit test. We can’t get too specific about what to expect from the call, as details like delay will change frequently. Let’s write the integration test in a separate test class. Create a file named AirportIntegrationTest.kt in the directory src/test/kotlin/com/agiledeveloper/airportstatus and key in the following code:

 package​ ​com.agiledeveloper.airportstatus
 
 import​ ​io.kotlintest.specs.StringSpec
 import​ ​io.kotlintest.data.forall
 import​ ​io.kotlintest.tables.row
 import​ ​io.kotlintest.matchers.string.shouldContain
 
 class​ AirportIntegrationTest : StringSpec() {
  init {
 "fetchData returns response from URL"​ {
  forall(
  row(​"IAD"​, ​"Dulles"​),
  row(​"SFO"​, ​"San Francisco"​),
  row(​"ORD"​, ​"Chicago"​)
  ) { code, partialName ->
  Airport.fetchData(code) shouldContain partialName
  }
  }
  }
 }

We test the fetchData() function for three different airport codes, to verify that the function returns a response that contains the partial name of the airport. Let’s replace the call to throw in the fetchData() function of the Airport class with the code to talk to the web service.

 fun​ ​fetchData​(code: String) =
  java.net.URL(​"https://soa.smext.faa.gov/asws/api/airport/status/$code"​)
  .readText()

If the network connection doesn’t fail, then as long as the web service at the URL is available and the airports don’t change their names, the integration test we wrote should pass. Run the tests to verify that all the tests are passing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.96.146