Junior — Middle
Kuidas te implementite oma koodi testimise JUnit-iga?
sobes.tech AI
Vastus AI-lt
JUnit is used for writing and running unit tests in Java. To test code with JUnit, you need to:
- Create a test class annotated with
@Testfor each test method. - In test methods, call the methods of the class being tested and verify the results using assertions like
assertEquals,assertTrue. - Run the tests via IDE or build system (Maven, Gradle).
Example of a simple test:
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calc = new Calculator();
int result = calc.add(2, 3);
assertEquals(5, result);
}
}
This approach helps automatically verify the correctness of the code during changes.