March 2015
Intermediate to advanced
236 pages
5h 26m
English
Let’s focus first on verifying how we populate an Address using the JSON response from the HTTP call. To do that, we’d like to change the behavior of HttpImpl’s get() method—just for purposes of the test we want to write—so that it returns a hardcoded JSON string. An implementation that returns a hardcoded value for purposes of testing is known as a stub.
HttpImpl implements the functional Http interface. Create a stub implementation dynamically using lambdas:
| iloveyouboss/mock-2/test/iloveyouboss/AddressRetrieverTest.java | |
| | Http http = (String url) -> |
| | "{\"address\":{" |
| | + "\"house_number\":\"324\"," |
| | + "\"road\":\"North Tejon Street\"," |
| | + "\"city\":\"Colorado Springs\"," |
| | + "\"state\":\"Colorado\"," ... |
Read now
Unlock full access