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 going to write our very first unit test using JUnit 5! What do you understand about what unit testing involves?
Is it just about testing if the code runs without errors?
Great question! While that's part of it, unit testing specifically checks whether individual components or units of code work as expected. Let's use a Calculator class as our example.
So, we'll be checking the methods like add, subtract, etc.?
Exactly! We'll start with the addition method and write a test to verify that adding two numbers gives the correct result.
How do we mark that method as a test in JUnit?
That's where the @Test annotation comes in. It indicates that this method is a test case. Let’s see how this looks in code.
Now, look at this example here. We have our test class CalculatorTest. Can anyone tell me what the method testAddition() does?
It tests the add method of the Calculator class, right?
Spot on! And notice how we create an instance of Calculator within the test? This allows us to call the method we want to test.
What about assertEquals? What does it do?
The assertEquals method is crucial! It checks whether the expected value, which is 5, matches the actual output from calc.add(2, 3). If they don't match, the test will fail!
So if I run this test and it doesn't equal 5, it means there's a bug in the add method?
Exactly! You’ll know that the addition functionality isn't working as it should.
Let’s talk about the message in assertEquals. Why do you think it’s important to include that?
I guess it explains why the test failed if it doesn’t pass.
Right! It provides clarity when tests fail, making it easier to understand what went wrong. Always aim for clear assertions.
Can you show us what that output looks like when we run the tests?
Sure! An IDE will show you the results of tests, indicating which ones passed or failed, and displaying any messages from failed assertions.
When you want to see if code runs true, test with JUnit, it's easy to do!
Imagine a wizard crafting spells in a lab. Each spell (method) must be tested to ensure it works as intended before it can be cast in public. JUnit is the wizard's magical tool that ensures each spell is tested perfectly!
Remember SEA for unit tests: Setup, Execute, Assert!
J.E.S.U.S - JUnit, Each method, Should be Understood and tested to ensure accuracy!
Term
What annotation is used to mark test methods?
Click to flip
Term
What does assertEquals do?
Click to flip
Term
What is the purpose of a unit test?
Click to flip
This section teaches how to create a basic unit test in JUnit 5 using a Calculator example to validate the addition functionality.
The section demonstrates writing a simple unit test in JUnit 5, using the Calculator class to check the correctness of the addition method by asserting the expected result. It highlights the key components of a test method including annotations for marking the test and assertions for verifying results.
In this section, we will learn how to write our first unit test using JUnit 5 by creating a simple test for a Calculator class. Below is a breakdown of what we will cover:
CalculatorTest that will contain a test method for the addition functionality of a Calculator class.@Test annotation provided by JUnit to indicate that the testAddition method is a test case.assertEquals(expected, actual) method from JUnit will be employed to verify that the result of the addition operation matches our expected outcome. The assertion will check that adding 2 and 3 gives us 5, which we assume is correct.import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorTest {
@Test
public void testAddition() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3), "2 + 3 should equal 5");
}
}assertEquals is used to check that the actual output from the method being tested matches the expected output, allowing us to validate the functionality and correctness of the code.Understanding and using these components is fundamental for effective unit testing with JUnit.