01Chapter 1: Why Write Unit Tests?Every program has bugs at some point. The hard part is finding them early, while they are still cheap to fix. Unit tests help you do that.2 min read
02Chapter 2: Setting Up JUnit 5 in Your ProjectBefore you can write a test, your project needs the JUnit 5 library. This chapter shows how to add it with Maven or Gradle and where your test files should go.2 min read
03Chapter 3: Your First JUnit TestIn the last chapter you added JUnit 5 to your project. Now you will write a real test. A test is a small piece of code that runs your program code and checks that it gave the right answer. JUnit runs the test for you and reports whether it passed or failed.2 min read
04Chapter 4: Running Tests and Reading the ResultsYou have written a test. Now you need to run it and understand what JUnit tells you. You can run tests in two places: your IDE and your build tool. An IDE (Integrated Development Environment) is a program for writing code, such as IntelliJ IDEA, Eclipse, or VS Code. A build tool is a program that co2 min read
05Chapter 5: Checking Results with AssertionsA test needs a way to say "this result is right" or "this result is wrong." In JUnit you do that with assertions. An assertion is a method call that checks a value. If the check passes, nothing happens and the test keeps going. If the check fails, JUnit stops the test and marks it as failed.2 min read
06Chapter 6: Testing That Code Throws ExceptionsGood code does more than return correct answers. It also refuses bad input. When something goes wrong, Java code often throws an exception. An exception is an object that signals an error and stops normal execution. A test should check that this happens at the right time, with the right type and the2 min read
07Chapter 7: Structuring and Naming TestsA test that passes today will be read again months from now, often when it fails. A clear structure and a clear name tell the reader what went wrong without making them study the code. This chapter shows a simple shape for every test and a few ways to name tests well.2 min read
08Chapter 8: Setup and Teardown with Lifecycle MethodsMany tests need the same starting objects. You could build them again inside every test, but then the same lines repeat everywhere. JUnit gives you lifecycle methods for this. A lifecycle method is a method that JUnit runs automatically before or after your tests.2 min read
09Chapter 9: Running One Test with Many InputsSometimes you want to check the same behavior with many different values. You could copy the same test five times and change the numbers each time, but that makes the test file long and easy to get wrong. JUnit gives you a better tool: the parameterized test.2 min read
10Chapter 10: Skipping, Tagging, and Conditional TestsSometimes you do not want every test to run every time. A test might be broken while you fix a bug. A slow test might not need to run on every save. A test might only make sense on Windows. JUnit 5 gives you three tools for these cases: @Disabled, @Tag, and assumptions.2 min read
11Chapter 11: Grouping Tests with @NestedAs a test class grows, its methods turn into one long list. Seeing which tests belong together gets hard. JUnit 5 lets you sort tests into groups with @Nested.2 min read
12Chapter 12: Putting Time Limits on TestsMost unit tests finish in a few milliseconds. Sometimes, though, a bug makes code run far too long, or it never finishes. An endless loop is one example. A test that hangs can block your whole build. JUnit lets you set a timeout, which is a time limit for a test. If the code takes longer than th2 min read
13Chapter 13: Testing File Code with @TempDirSome code reads and writes files. Testing that code takes some care. If a test writes into your real project folder, it can leave junk files behind. It can even overwrite something important. JUnit 5 solves this with @TempDir.2 min read
14Chapter 14: Isolating Code with MocksReal classes depend on other classes. A checkout service might ask a repository for prices. A repository is a class that loads data, often from a database. If your test uses the real repository, the test needs a running database. It becomes slow, and it can fail for reasons that have nothing to do w2 min read
15Chapter 15: Extending JUnit with @ExtendWithAn extension is a class that adds extra behavior to JUnit. JUnit calls the extension at set moments while your tests run, such as before each test or after each test.2 min read
16Chapter 16: Writing Tests That Stay UsefulA test is useful when it fails only for a real reason and tells you clearly what broke. A badly written test does the opposite. It fails at random, runs slowly, or breaks every time you tidy up your code. This chapter covers a few habits that keep your tests worth having.2 min read
17Chapter 17: Coming from JUnit 4Many Java projects still have tests written with JUnit 4. JUnit 4 is the older version of JUnit, first released in 2006. This chapter shows the main differences between JUnit 4 and JUnit 5, so you can read old tests and move them to the new style.2 min read