Java JUnit: Writing Test Cases – PL Courses

Java JUnit: Writing Test Cases – PL Courses

JUnit is a popular testing framework for Java that allows developers to write and run tests for their code easily. Unit testing is a important aspect of Test-Driven Development (TDD), where tests are written before the actual code. In this article, we will learn how to write test cases in Java using JUnit.

To get started with writing JUnit test cases, you first need to set up JUnit in your project. You can do this by adding the JUnit dependency in your project’s build file (usually pom.xml for Maven or build.gradle for Gradle).

Once you have JUnit set up, you can start writing test cases. A test case in JUnit is a method annotated with @Test. This method contains the code that tests a specific functionality of the class you want to test.

Below is an example of a simple test case:

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {

    @Test
    public void testAddition() {
        Calculator calculator = new Calculator();
        assertEquals(5, calculator.add(2, 3));
    }
}

In the above example, we have a test class CalculatorTest that tests the add method of the Calculator class. We use the assertEquals method from JUnit’s Assert class to check if the actual result of the addition method matches the expected result.

JUnit provides several other assertion methods such as assertTrue, assertFalse, assertNull, assertNotNull, and assertArrayEquals, among others, to check different types of conditions.

Besides @Test, JUnit provides other annotations to help structure your tests:

  • @Before – marks a method to be run before each test case.
  • @After – marks a method to be run after each test case.
  • @BeforeClass – marks a static method to be run once before all test cases in the class.
  • @AfterClass – marks a static method to be run once after all test cases in the class.
  • @Ignore – tells JUnit to skip the test case.

Here’s how you can use these annotations:

import org.junit.*;

public class CalculatorTest {
    
    private Calculator calculator;
    
    @Before
    public void setUp() {
        calculator = new Calculator();
    }
    
    @After
    public void tearDown() {
        calculator = null;
    }
    
    @Test
    public void testAddition() {
        assertEquals(5, calculator.add(2, 3));
    }
    
    @Ignore
    public void ignoredTest() {
        // This test will not be run
    }
    
    @BeforeClass
    public static void beforeClass() {
        System.out.println("This runs once before all tests");
    }
    
    @AfterClass
    public static void afterClass() {
        System.out.println("This runs once after all tests");
    }
}

In the above example, the @Before annotation is used for setting up a new instance of Calculator before each test case. Correspondingly, the @After annotation is used to clean up after each test case.

The @BeforeClass and @AfterClass annotations are useful for setting up and tearing down expensive resources that are shared across all test cases in that class.

The @Ignore annotation enables you to skip a test case that may be under construction or not relevant at the moment.

In conclusion, writing unit tests using JUnit in Java enables you to ensure your code functions correctly and helps prevent future regressions. It’s a good practice to write tests alongside your code and run them frequently. This gives you confidence in your code and can save time debugging in the long run.

Source: https://www.plcourses.com/java-junit-writing-test-cases/


You might also like this video