Course info
Nov 8, 2018
1h 28m

Unit test frameworks like JUnit 5 help guide the design of your code as well as ensure that it works. In this course, Getting Started Unit Testing with JUnit 5, you'll get up to speed on the key skill of unit testing by leveraging the latest version of the popular JUnit unit test framework, JUnit 5. First, you'll learn how to validate your code with assertions and leverage JUnit 5 lifecycle methods. Next, you'll explore how to categorize tests, test exception handling, produce clear test reports, and many other JUnit 5 capabilities. Finally, you'll delve into two techniques critical to learn for unit testing - how to handle pre-existing untested code and how to drive development from a unit test. By the end of this course, you'll have the necessary knowledge to efficiently begin unit testing your own projects with JUnit 5.

About the author
About the author

Jim Weaver is a software developer and mentor with a love for small teams, light-weight processes, and close interaction with customers.

More from the author
Jira: Getting Started
2h 37m
Mar 20, 2019
Unit Testing Legacy Code in Java
2h 8m
May 21, 2018
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi, I'm Jim Weaver for Pluralsight, and I've been writing business-critical code in a variety of industries for decades. I've relied on unit test frameworks like JUnit for much of that time to help guide the design of my code, as well as ensure that it works. JUnit 5 is the latest incarnation of the most popular Java unit test library, and in this beginner-level course, Getting Started Unit Testing with JUnit 5, I'll introduce you to both unit testing and the JUnit 5 library. The goal of this course is to get you ready to unit test your own Java projects with JUnit 5 as rapidly as possible. We'll get JUnit 5 working with a realistic project and write our first test quickly. Then we'll dive into the most essential and useful features of JUnit 5 as we add functionality to that system, assertions you can use to validate your code, test setup and teardown methods, producing clear test reports, running categories of tests, and dealing with exceptions. JUnit 5 makes use of Java 8 lambda support, and we'll see that in action while testing our project as well. We'll also learn how to deal with some common real-world issues when trying to write tests for existing code, code that has mixed concerns and code with dependencies that are making your tests difficult. Finally, we'll use JUnit 5 to kick the tires on test-driven development. When we're done, you'll be ready to test your projects with JUnit 5 and also to learn more through the JUnit 5 Fundamentals course. So come join me for Getting Started Unit Testing with JUnit 5.

Writing Your First Test
Welcome to Getting Started Unit Testing with JUnit 5. This is a beginner-level course. You 'll need a rudimentary understanding of Java syntax and object-oriented basics, but no in-depth Java experience or prior experience with JUnit is needed. If you're very experienced with both Java and unit testing and you're just looking to learn about the newest version of JUnit, then the JUnit 5 Fundamentals course here at Pluralsight is the best fit for that. The goal in this first module is to get our first unit test written with JUnit 5. If you have existing code of your own that you're hoping to learn how to unit test with this course, just this first module should get you started. Before writing our first test, we'll briefly discuss the benefits of unit testing. That will provide some context for the rest of the course. We won't spend too much time on this. Since you're here taking this course, you probably already have some idea of the value of unit testing. Next, we'll configure two popular Java IDEs to run JUnit test with. Those will be IntelliJ and Eclipse. Then we'll write our first JUnit 5 test against a small patient appointment system. And even though that will be a simple test, it should be enough to get you rolling on your own project or assignments just from this module.

Writing More Complex Tests
Now that we have JUnit 5 set up and working, it's time to explore its capabilities more thoroughly by writing some more complex tests for our patient intake system. In this module, we'll expand our knowledge of JUnit 5 assertions. We'll learn how to control test execution, including disabling tests and making use of tests lifecycle methods. We'll learn how to test for exceptions in those cases where you need to verify that an error is thrown for a certain scenario. We'll customize how test results are reported, making it easier to understand what has been tested or what the failures mean. Finally, we'll see how to categorize tests with JUnit 5 and run particular categories of tests rather than all the tests. After this module, you'll have a very good basis to test your own applications with JUnit 5. Before we start digging in, it's worth noting that the JUnit 5 documentation we saw in the previous module also includes API docs, and in particular, the JUnit 5 Assertions class has all the static methods that comprise JUnit 5's assertions. You can see that there are many flavors of assertEquals, for example, for comparing different data types, but there are also some more interesting ones, and some of the most useful ones we'll learn in this module. This is a great source of detailed information of assertions for you to refer to. Also, as we continue to work with our patient intake system throughout this module, the source code we're working with can be found on GitHub. The location is at my GitHub account for weaver, the gettingstartedjunit5 repository, and there will be branches with the state of the code in between each module. The master branch is after the first module. Okay, let's learn some more about what we can do with JUnit 5.

Making Existing Code Testable
We've seen and applied many of the features of JUnit 5 at this point, but if you're just starting out unit testing, the biggest problem you face typically isn't learning the specifics of a testing library, but rather, just getting access to the code you're trying to verify from test code. Applying unit tests to code that's already written is not always easy, but there are a few principles and techniques that can help immensely. In this module, we'll learn how to tackle two common issues that make code difficult to test and that anyone new to unit testing will be likely to encounter on day one, code with mixed concerns, that is code that's doing more than one thing, and code with problematic dependencies. We'll continue applying JUnit 5's capabilities in this module as we work through these common situations in the context of our patient intake system.

Writing Tests First
In the previous module, we wrote JUnit 5 tests for code that was already written in our patient intake system. In this module, we're going to try test-driven development using JUnit 5. Test-driven development involves writing tests first, before the code that you're testing is written yet. That might sound like crazy talk, but it's actually a beneficial technique that's been around and in practice for almost two decades. First, we'll learn a little about test-driven development, just enough to get us started. Then we'll develop a small new feature for our patient intake system, but by driving development of that feature from a unit test.