Test-Driven Development (TDD) is a discipline that helps to create error-free, quality code, and drives productivity by reducing and eliminating errors as well as providing many other benefits.
In these video tutorials, you will learn about TDD, its key principles and practices, and be introduced to test-driven development with JUnit5.
Select play to begin your journey through the following tutorials.
- Test-Driven Development [4m 47s]
- Unit Testing [7m 11s]
- JUnit5 Introduction [7m 12s]
- Java Bitcoin Library Design Review and Tooling Setup [4m 48s]
- Initial Project Setup Using Maven and First Set of JUnit5 Unit Tests [24m 3s]
Welcome back. In this lesson, we're going to update our implementation so that rather than returning fictitious values back for our GetExchangeRate method, we'll actually connect out to the CoinDesk Bitcoin API and actually call the following URL to get real-time information about exchange rates.
So you can see here that this URL returns a payload of JSON, which contains the conversion rates for the various currencies. So for USD, this is the current conversion rate. For Great British pounds, this is the current conversion rate. And for euros, this is the current conversion rate.
In fact, if we take this and we jump into my local terminal, and if I were to do a curl -s to that URL and then pipe the results to jq, you get a better understanding of the format of the returned JSON payload. So again, there are currencies, and for each currency, there is a rate that we'll need to parse out.
Okay, let's get started. So back within Visual Studio Code, first thing I'll do is I'll set up a new field. It'll be private, and it will represent the URL of the API that we need to call. I'll then create a new HttpGet object initialized with that URL.
Now, for this to work, we'll need to import the following types. The first four types are all required to do our HTTP connectivity to CoinDesk's Bitcoin API. The remaining types that we are importing are there to help us actually parse the response or the payload returned from the call to CoinDesk's Bitcoin API. We'll then need an HTTP client to allow us to connect to CoinDesk's Bitcoin API. And then, for the GetExchangeRate, I'll simply overwrite the current implementation with the following one.
In essence, what this is doing is that it is connecting or executing a call to the CoinDesk Bitcoin API, and then it is parsing the response and extracting the rate. The particular rate it extracts is based on the currency string passed in.
Now, if an exception happens anywhere within this code, we'll just return and set the rate to be negative one. And that's it. So let's save that. And now, let's run our test, which I expect them to fail, as they do. And the reason being is we haven't configured in the new third-party dependency libraries that our source code now has. So let's do that.
So under dependencies, I'll paste in the following. So on the step two branch, these are our new dependencies that we have. So the first one provides us our ability to perform HTTP gets to the CoinDesk Bitcoin API, and the second one is provided so that we can parse the JSON payload that is contained in the response. So we'll copy this here. I'll return to our source code, and I'll paste it like so. I'll save this.
Now, I'll rerun the tests. And this is still failing. So this is not a bad thing. This is all part of the red, green, refactor workflow. It's telling us that our implementation isn't quite correct. So when reviewing the code, what I realize I've missed is that I need to initialize the HTTP client. I'll save that, clear the terminal, rerun our tests. Again, the tests have failed.
So this time when we review the test results, the interesting thing is that we are actually getting live results back. However, our expectations are still using the old ones from the previous lesson. So the fact that these tests are failing is an expected result.
Now, we could go back into our unit tests and update our expected results with these values here, but keep in mind that these currency rates are changing in real time, and at the moment, our unit tests have become integration tests because they're making external connections to an external dependency, and we don't want that for our unit tests. Therefore, in the next lesson, you'll learn about mocking, and how we can mock out and simulate the external dependency.
Go ahead and close this lesson, and I'll see you shortly in the next one.
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.