In this section, we will walk through creating your first JUnit test. This will include setting up a simple Java project, writing a basic test case, and running the test to see the results.
Step-by-Step Guide
- Setting Up Your Project
Before writing your first JUnit test, ensure you have JUnit set up in your project. If you haven't done this yet, refer to the previous section on Setting Up JUnit.
- Writing Your First Test Case
Let's start by creating a simple Java class that we want to test. For this example, we'll create a class called Calculator
with a method add
that adds two integers.
Calculator.java
Now, let's write a JUnit test for the add
method.
CalculatorTest.java
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { Calculator calculator = new Calculator(); int result = calculator.add(2, 3); assertEquals(5, result, "2 + 3 should equal 5"); } }
Explanation of the Test Code
-
Import Statements:
import static org.junit.jupiter.api.Assertions.assertEquals;
- This imports theassertEquals
method, which is used to check if the expected result matches the actual result.import org.junit.jupiter.api.Test;
- This imports the@Test
annotation, which is used to mark a method as a test method.
-
Test Class:
public class CalculatorTest
- This is the test class where we will write our test cases.
-
Test Method:
@Test
- This annotation indicates that thetestAdd
method is a test method.public void testAdd()
- This is the test method where we will write the test logic.Calculator calculator = new Calculator();
- We create an instance of theCalculator
class.int result = calculator.add(2, 3);
- We call theadd
method and store the result.assertEquals(5, result, "2 + 3 should equal 5");
- We use theassertEquals
method to check if the result is 5. If the result is not 5, the test will fail, and the message "2 + 3 should equal 5" will be displayed.
- Running the Test
To run the test, you can use your IDE's built-in test runner or a build tool like Maven or Gradle. Here, we'll show how to run the test using an IDE (e.g., IntelliJ IDEA or Eclipse).
Running the Test in IntelliJ IDEA
- Right-click on the
CalculatorTest
class in the Project Explorer. - Select
Run 'CalculatorTest'
.
Running the Test in Eclipse
- Right-click on the
CalculatorTest
class in the Package Explorer. - Select
Run As
->JUnit Test
.
- Viewing the Test Results
After running the test, you should see the test results in the Test Runner window of your IDE. If the test passes, you will see a green bar indicating success. If the test fails, you will see a red bar indicating failure, along with an error message.
Practical Exercise
Exercise 1: Write a Test for the Subtract Method
- Add a
subtract
method to theCalculator
class.
- Write a JUnit test for the
subtract
method in theCalculatorTest
class.
@Test public void testSubtract() { Calculator calculator = new Calculator(); int result = calculator.subtract(5, 3); assertEquals(2, result, "5 - 3 should equal 2"); }
Solution
@Test public void testSubtract() { Calculator calculator = new Calculator(); int result = calculator.subtract(5, 3); assertEquals(2, result, "5 - 3 should equal 2"); }
Common Mistakes and Tips
-
Common Mistake: Forgetting to annotate the test method with
@Test
.- Tip: Always ensure your test methods are annotated with
@Test
so that the test runner can recognize and execute them.
- Tip: Always ensure your test methods are annotated with
-
Common Mistake: Using incorrect assertion methods.
- Tip: Familiarize yourself with the different assertion methods provided by JUnit, such as
assertEquals
,assertTrue
,assertFalse
, etc.
- Tip: Familiarize yourself with the different assertion methods provided by JUnit, such as
Conclusion
In this section, you learned how to write and run your first JUnit test. You created a simple Calculator
class, wrote a test case for the add
method, and ran the test to verify its correctness. You also practiced writing a test for the subtract
method. In the next module, we will dive deeper into JUnit annotations and their usage.
JUnit Course
Module 1: Introduction to JUnit
Module 2: Basic JUnit Annotations
- Understanding @Test
- Using @Before and @After
- Using @BeforeClass and @AfterClass
- Ignoring Tests with @Ignore
Module 3: Assertions in JUnit
Module 4: Parameterized Tests
- Introduction to Parameterized Tests
- Creating Parameterized Tests
- Using @ParameterizedTest
- Custom Parameterized Tests
Module 5: Test Suites
Module 6: Mocking with JUnit
Module 7: Advanced JUnit Features
Module 8: Best Practices and Tips
- Writing Effective Tests
- Organizing Test Code
- Test-Driven Development (TDD)
- Continuous Integration with JUnit