In this tutorial, you will learn about JUnit5 Lifecycle methods. A Lifecycle Method is any method that is directly annotated or meta-annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach. Let’s have a look at these annotations one by one. @BeforeAll When a method is annotated with @BeforeAll it means that the method should be executed before…
Read More A Guide to JUnit5 Lifecycle Methods
What is JUnit 5? JUnit 5 is actually a combination of three main elements. It’s a mix of JUnit Platform, JUnit Jupiter API, and JUnit Vintage API. Let’s break it down. JUnit Platform Think of JUnit 5 as having a solid foundation called the JUnit Platform. This platform is like the engine that gets your…
Read More What is JUnit 5? (+Video Tutorial)
This tutorial will guide you through the process of running Unit Tests and Integration Tests separately in a Maven-based Spring Boot application. We will use the JUnit Tag feature to group our test cases and separate Unit Tests from Integration Tests. To learn more, check out the Test Java code tutorials page. Create your tests…
Read More Running Unit Tests and Integration Tests Separately in Spring Boot
In this tutorial, you will learn how to use the @Tag annotation in JUnit 5 for organizing and running your unit tests. By the end of this tutorial, you’ll have a solid understanding of the @Tag annotation, its benefits, and how to apply it effectively in your own test cases. Let’s dive in! What is…
Read More @Tag Annotation: Organizing and Running Tests in JUnit 5
Testing is an essential skill for any Java developer. It helps you ensure the quality and reliability of your code, as well as find and fix bugs faster. On this page, you will find a collection of tutorials that will teach you how to test Java code using JUnit 5 and Mockito. JUnit 5 is…
Read More Testing Java Code
A test suite is a collection of test cases that are run together as a single unit. It helps to organize and manage the execution of multiple test cases. Test suites can be used to group related tests or to test different aspects of a complex system. They can also be used to run tests…
Read More JUnit 5 Test Suite Tutorial
In this tutorial, you will learn how to write a JUnit test that validates whether the method under test throws the correct exception. The tutorial includes examples for both JUnit 5 and JUnit 4. For video lessons, check out my video course: Testing Java with JUnit and Mockito. Let’s begin with JUnit 5 version first.…
Read More Test for Exception in JUnit 5 and JUnit 4
JUnit is a popular unit testing framework for Java. In JUnit 5, assertions are used to check the expected outcome of a test case. Assertion messages are used to provide more information about the failed test and help in debugging. The Lazy Assertion Message feature in JUnit 5 allows you to specify the assertion message…
Read More JUnit 5 Lazy Assertion Message Tutorial
In this tutorial, we will discuss how to Mock Objects and Stub Methods with Mockito, and we will see various ways to stub with common scenarios and examples. Mocking and stubbing are the basis of writing clean and simple unit tests. So, let’s begin! Visit our Mockito introductory level tutorial if you are a beginner…
Read More How to Mock Objects and Stub Methods with Mockito?
In this tutorial, we’ll implement a RestTemplate Error Handling Example. But before moving ahead, let’s see why is there a need to do this? The answer is: It becomes very tedious to handle exceptions through try/catch blocks as applications scale. An Efficient way to avoid this hassle is to implement a reusable error handler that…
Read More RestTemplate Error Handling Example
This tutorial will teach you how to use TestRestTemplate to send HTTP Post requests. If you are also interested to learn how to send an HTTP GET request, then please have a look at the “Get List of Objects with TestRestTemplate” tutorial. What is TestRestTemplate? TestRestTemplate is HTTP Client that you use for Spring Boot…
Read More TestRestTemplate HTTP Post Example
This article will explore the JUnit 5 Assertions API, go in-depth with examples of building fundamental assertions, and look at customizing the error message that appears when an assertion fails. We’ll also discuss how to execute many assertions simultaneously as an assertion group in our last section. So, let’s begin! What are JUnit Assertions? As…
Read More An Overview of JUnit 5 Assertions with Examples
In a real-world scenario, when testing a system, there are some instances in which a test should only be executed under a specific environment. Integration tests that rely on a third-party system such as Operating System are one example. If that system goes down, we’ll omit the tests that need it to avoid build failure.…
Read More A Guide to Conditional Tests Execution Using JUnit 5
In this tutorial, you will learn about Unit Testing AWS Lambda Function with Java. First, we will have a quick overview of Unit Testing. Then we will look at the required dependencies in order to do Unit Testing of AWS Lambda Function. After that, we will dive deep into the annotations that will be used…
Read More Unit Testing AWS Lambda Function with Java
In this guide, you will learn to use @RepeatedTest and @ParametrizedTest annotations introduced in JUnit 5. We’ll look at the capability that lets us run a test a certain number of times. We’ll also look at interfaces that will allow us to retrieve information about the repeated tests. Moreover, we will also look at a…
Read More A Guide to @RepeatedTest and @ParametrizedTest in JUnit 5