Unit testing with JUnit
1. Unit testing with JUnit
Hello again! We will now learn about unit testing and the main use cases for JUnit!2. Unit testing - definition
Let's start with two key definitions - unit and unit testing. In software development, a "unit" refers to the smallest testable part of an application, like a method. A unit test then checks that a single unit of code works correctly on its own.3. Unit testing is about details
Unit testing is important because it checks the building blocks of an application, not the full system. Broader concerns are handled by other tests, like integration tests. JUnit is named for unit testing. While it supports various test types, its main use is unit testing.4. Types of testing - brief overview
Keep note of this picture - while it does not provide a full list of all types of testing, these are the primary types software engineers will encounter on the job.5. Types of testing - brief overview
In this introductory course, we will discuss mostly unit and some integration testing.6. JUnit syntax: assertTrue() and assertFalse()
Knowing that, let's learn more ways to write verifications in unit tests beyond `assertEquals()`! Another important JUnit assertion is `assertTrue`, which checks if a boolean expression is true. A common use is verifying if a collection is empty since unexpected emptiness can cause major issues. In this snippet, we create an empty list and we assert it's empty. And here, with `assertFalse`, we check that another list is not empty.7. assertNull() and assertNotNull()
Just as an empty collection can be a problem, an unexpected null value can lead to `NullPointerExceptions`! JUnit provides us with ways to test that a variable is or isn't null - `assertNull` and `assertNotNull`. A common use case is checking a value retrieved from somewhere (for example a map) was found and therefore is not null.8. Asserting exceptions
Verifying exceptions is important for writing resilient code. Suppose we want to verify this method will return a valid output in some situations and throw an `ArrayIndexOutOfBoundsException` in others. JUnit provides multiple ways to do this.9. assertInstanceOf()
The most straightforward ways to do this is `assertThrows()` but it uses advanced syntax. More is explained in the citation. We can use a try/catch to capture exceptions, and then use `assertInstanceOf()` with the expected class of the exception and the caught exception object to verify what type of exception is thrown.10. Naming unit tests
A few final words: A typical project contains hundreds of unit tests. Unit tests should be lightweight and simple to understand. They should have informative names so that we can see at first glance what went wrong if one of them fails. The citation contains some popular unit test naming conventions. While Java doesn't normally use underscores, some unit test naming conventions for Java do. The one we will use in this course is methodUnderTest_expectedBehavior_conditions.11. Let's practice!
Now, let's now use your knowledge to write unit tests!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.