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

  1. 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.

  1. 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

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

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 the assertEquals 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 the testAdd 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 the Calculator class.
    • int result = calculator.add(2, 3); - We call the add method and store the result.
    • assertEquals(5, result, "2 + 3 should equal 5"); - We use the assertEquals 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.

  1. 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

  1. Right-click on the CalculatorTest class in the Project Explorer.
  2. Select Run 'CalculatorTest'.

Running the Test in Eclipse

  1. Right-click on the CalculatorTest class in the Package Explorer.
  2. Select Run As -> JUnit Test.

  1. 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

  1. Add a subtract method to the Calculator class.
public int subtract(int a, int b) {
    return a - b;
}
  1. Write a JUnit test for the subtract method in the CalculatorTest 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.
  • Common Mistake: Using incorrect assertion methods.

    • Tip: Familiarize yourself with the different assertion methods provided by JUnit, such as assertEquals, assertTrue, assertFalse, etc.

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.

© Copyright 2024. All rights reserved