Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're learning how to write our first unit test in JUnit 5! First, can anyone tell me what a unit test is?
Is it a way to test small parts of code?
Exactly! A unit test checks the functionality of the smallest parts of our code. JUnit 5 is a framework that makes this process easier. Now, let’s go over the basic structure of a unit test.
What are the main components we need to include?
Great question! The main components include the @Test annotation, the assertion methods, and, optionally, setup and teardown methods. We'll see these in action shortly!
Let’s write a test to verify the addition function of a Calculator. Here’s a simple example: @Test public void testAddition(){ Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); } Can anyone identify the key components here?
The @Test annotation and the assertEquals method!
That's correct! The @Test marks it as a test method, and assertEquals checks if our actual result matches the expected result. Is everyone clear so far?
What happens if the test fails?
If it fails, JUnit will indicate the failure and show the expected versus actual values, helping us find bugs. This is why writing unit tests is so beneficial!
Let's review what we've learned today. What is the purpose of the @Test annotation?
It marks a method as a test method!
Exactly! And why do we use assertions?
To check if the output of a method is what we expect.
Perfect! Writing unit tests helps us catch errors early in the development process, ensuring our code works as intended.
JUnit will put to the test, every code bit that is best!
Imagine a small calculator in a big world of software. One day, it decided to test its two plus three operation, marking it with a special tag called @Test and declaring, 'Let’s see if I truly make five!'
A for Annotation, A for Assert, L for Listeners - remember these in JUnit.
JUnit is like J(A)st Unifying New Intuitive Tests - remember that testing helps unify understanding!
Term
What does @Test represent in JUnit?
Click to flip
Term
What does assertEquals do?
Click to flip
Term
What is JUnit 5?
Click to flip
This section provides an overview of writing a unit test using JUnit 5, detailing its structure and key components.
In this section, we learn how to write our first unit test in JUnit 5 for a simple Calculator class. The focus is on the structure of the unit test, which includes annotations, setup, execution, and assertions to verify expected outcomes.
In this part of the chapter, we delve into the practical aspect of unit testing by writing our first test case using JUnit 5, a popular framework for unit testing in Java. The key components of a unit test include:
assertEquals(expected, actual), we can verify that the output from the method matches the expected result. In our example, we test the addition method of a Calculator class by checking if adding 2 and 3 yields 5.This section is crucial because it sets the foundation for writing more complex tests and understanding the framework’s capabilities.