The course is part of this learning path
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.
This entry-level training course is designed to bring you quickly up to speed with the basic features and processes involved in performing Java based test-driven development using JUnit5. It covers the key principles and practices of test-driven development, which you can then utilize within your own software engineering projects.
In this course, you'll be guided through an end-to-end sample project in which we use test-driven development to build, test, and package a Java 11 based library that will provide an API to convert Bitcoins into foreign currency.
If you have any feedback relating to this course, please contact us at email@example.com.
- Understand the fundamentals of TDD and JUnit5
- Use TDD to build, test, and package a Java 11 based library
- Perform TDD by performing Red-Green-Refactor iterations
- Learn how to use TDD to build, test, and package a Java 11 based library
- Learn how to create unit tests using the JUnit5 testing framework
- Software developers interested in using TDD to build JDK11 based applications using Java
To get the most out of this course, you should have a basic understanding of JDK11 and the Java programming language, as well as software development and the software development life cycle (SDLC).
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.
Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, GCP, Azure), Security, Kubernetes, and Machine Learning.
Jeremy holds professional certifications for AWS, GCP, and Kubernetes.