1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Introduction to Test Driven Development (TDD) - for Java using JUnit

Build Client Console Application to Import GitHub Action Built Bitcoin Library Artifact

Start course
Overview
Difficulty
Intermediate
Duration
1h 49m
Students
213
Ratings
3.6/5
starstarstarstar-halfstar-border
Description

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 support@cloudacademy.com.

Learning Objectives

  • 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

Intended Audience

  • Software developers interested in using TDD to build JDK11 based applications using Java

Prerequisites

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).

Resources

Transcript

Welcome back. In the previous lesson, we finished off with a official release of our Bitcoin computer service library. And we downloaded it to the local file system. So in this last demonstration, what I wanna do is show you how to consume that library in a Java console application. With the end result being a Java application that we run on the command line, that takes user input in the form of two parameters. The first one being a foreign currency, and the second one being the number of bitcoins that we wanna convert into the currency. Let's begin.

So I'll jump into Visual Studio code. And I'll create another folder and park it up next to the Bitcoin converter library project. This time I'll call it client, Internally integrated terminal, I'll navigate into client. And here I'll make another directory called libs. And this will store any libraries that this application depends on. In this case, we'll copy and now Bitcoin converter service library.

Since we're also going to use Maven to build this Java console web Will use the same folder structure that Maven expects. Therefore, I'll create a directory structure called source, main, Java, com, Cloud Academy, Bitcoin. And we'll finish with client. Okay, that looks good. And next, we'll add in a POM file. From here, I'll navigate over to the course repo. And this time, I'll click on the step seven branch. I'll navigate into client and then I'll click on the POM file and got a rule. And I'll just select everything. Paste the POM file in here.

Now, this is going to produce our client will leave the version is a snapshot version. And, you can see here that we're going to set up a main class, it's going to be called Bitcoin app. So in our source directory, we'll create this we'll jump back into our repo. And just keep the demonstration moving along quickly. I will click on a Bitcoin app java file. And I will copy the source for it. Okay, that looks good.

So here we can see all we're doing is we're starting up a while loop that will continually loop through, it will ask read from the console, a value for number of coins that we want to convert currency we want to convert it into. And then this particular line here is actually going to use our computer service library and it will connect out to the CoinDesk Bitcoin API and get the current rates, do the maths and then return the Dollar value. And then we simply print out the Dollar value to the console.

Okay, we'll save that. Now, the next thing we need to do is we need to grab our library. So jump back into my host terminal. And this time, I'm going to do a copy on downloads. And it's this file here, Bitcoin dash computer. And it's the Bitcoin dash converter dash service lib, release 1.0.0.jar file we want and we want to copy it into our Java TDD Bitcoin converter, client lib's directory.

Okay, jumping back into Visual Studio code, we can see that our vagrant instance has access to that. And at this stage, we're ready to start doing the compilation. But to do so, we need to actually install this JAR using Maven. So again, I'll jump back out to our instructions on the step seven branch. 

Next, we'll run each of these commands one by one so we understand what they're doing. So we're on the first one which seats a final variable. Figure that out. You can see that it captures the name of the library, which is up here. We then want to extract the version number, which is this number here out of the filename.

So, to do so I just need to update this regular expression because we're not operating on a snapshot version. So there's no snapshot at the end of it. So I'll delete that and run that. Again, we come out the version variable. And we can see that it has indeed captured our vision correctly.

Well now jump back over to our instructions. And we'll copy the Maven install command. Back with a new terminal, we'll paste it into. And Excellent. So that's installed our library into our Maven cache. Clear the terminal. And the final thing we need to do is update the dependency within the pom.XML file. So we'll do that now. We'll change this to be 1.0.0, which is the version that we set for our Bitcoin computer service library that saved.

Now at this stage, we can do a Maven clean, compile. And compilation is succeeded. So that looks good. And then the final thing we'll do is we'll do a Maven package. Now this will generate an executable JAR file for us, which is our Java console app. That's completed to a tray. We can say indeed, that we've got our Bitcoin converter client.

Okay, so the final thing to do is to actually start up a Java console application. To do so we use Java test jar, the executable jar is in the target directory, it is called Bitcoin converter client. And we wanna run the one with the dependencies baked into it into. Okay, looks good. So let's test it out. We'll say 300 Bitcoins, and we'll do Euro. And here we can see the live result.

So it looks good. Let's do 100 into USD. And again, we get the Dollar value and USD. So it's roughly 19 million. Let's jump into CoinDesk Bitcoin calculator. And we'll do the same amount 1000 Bitcoins. And indeed, we get 19 million. So that's brilliant.

So let's quickly summarize what we've accomplished throughout the given demonstrations. It was really about doing test driven development, and using unit test to drive the implementation of our Bitcoin service library, such that when we finally used it, we had the confidence that it would do exactly as it was designed to do. And that really is the key point about test driven development. Testing early, and then you can have the confidence there when you're in runtime, things are going to work as designed.

Okay, go ahead and close this lesson.

About the Author
Avatar
Jeremy Cook
Content Lead Architect
Students
67635
Labs
44
Courses
105
Learning Paths
51

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.