AllRounder.ai
Chapters in this course

Enrol to start learning

Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.

Enrol free

25.7. Writing Your First Unit Test with JUnit 5

Interactive Audio Lesson

Session 1: Introduction to Unit Testing with JUnit 5

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Today, we're going to write our very first unit test using JUnit 5! What do you understand about what unit testing involves?

Noah
Noah

Is it just about testing if the code runs without errors?

Sarah
SarahInstructor

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.

Isabella
Isabella

So, we'll be checking the methods like add, subtract, etc.?

Sarah
SarahInstructor

Exactly! We'll start with the addition method and write a test to verify that adding two numbers gives the correct result.

Akash
Akash

How do we mark that method as a test in JUnit?

Sarah
SarahInstructor

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.

Session 2: Examining the Test Code

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Robert
RobertInstructor

Now, look at this example here. We have our test class CalculatorTest. Can anyone tell me what the method testAddition() does?

Ananya
Ananya

It tests the add method of the Calculator class, right?

Robert
RobertInstructor

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.

Noah
Noah

What about assertEquals? What does it do?

Robert
RobertInstructor

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!

Isabella
Isabella

So if I run this test and it doesn't equal 5, it means there's a bug in the add method?

Robert
RobertInstructor

Exactly! You’ll know that the addition functionality isn't working as it should.

Session 3: Understanding Output and Assertions

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Let’s talk about the message in assertEquals. Why do you think it’s important to include that?

Akash
Akash

I guess it explains why the test failed if it doesn’t pass.

Sarah
SarahInstructor

Right! It provides clarity when tests fail, making it easier to understand what went wrong. Always aim for clear assertions.

Ananya
Ananya

Can you show us what that output looks like when we run the tests?

Sarah
SarahInstructor

Sure! An IDE will show you the results of tests, indicating which ones passed or failed, and displaying any messages from failed assertions.