In Java, test cases are typically written using JUnit, a popular testing framework. To write a test case, follow these steps:
- Set up JUnit: Add JUnit dependency in your project.
- Create a test class: Annotate it with
@Test
. - Write test methods: Each method should test one unit of functionality.
- Assertions: Use assertions like
assertEquals()
,assertTrue()
,assertFalse()
, etc., to verify expected results.
Example:
This tests the add()
method of a Calculator
class.