MCA
JUNIT TESTING // ENTERPRISE CODE // ABSTRACT FACTORY // JUNIT TESTING // ENTERPRISE CODE // ABSTRACT FACTORY //
BACK TO SYLLABUS
MEDIUM

JUNIT TESTING

Unit testing and test-driven development

CONCEPTS

01JUnit Annotations (@Test, @BeforeEach)
02Writing Assertions
03Testing exceptions
04Parameterized Tests
05Mocking with Mockito
06TDD methodology

SYNTAX_DEMO

Ensuring code quality
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class MathUtils {
    static int add(int a, int b) { return a + b; }
}

public class MathUtilsTest {
    
    @Test
    void testAdd() {
        assertEquals(5, MathUtils.add(2, 3), "2 + 3 should equal 5");
        assertNotEquals(0, MathUtils.add(1, 1));
    }
}