JUnit is a Java testing framework for automated unit testing, allowing developers to test individual methods efficiently. It utilizes annotations like @Test and assertions to facilitate the testing process, while also supporting features such as parameterized and repeated tests. JUnit is commonly used in software development for its reliability and integration with build tools like Maven and Jenkins.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views12 pages
JUnit Full Presentation Slides
JUnit is a Java testing framework for automated unit testing, allowing developers to test individual methods efficiently. It utilizes annotations like @Test and assertions to facilitate the testing process, while also supporting features such as parameterized and repeated tests. JUnit is commonly used in software development for its reliability and integration with build tools like Maven and Jenkins.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
What is JUnit?
• JUnit is a Java testing framework used for unit
testing. It allows you to test small parts (methods) of your program automatically instead of doing it manually. Why Use JUnit? • - Saves time by running tests automatically • - Catches bugs early • - Works with build tools like Maven/Jenkins • - Helps in Test-Driven Development (TDD) • - Repeatable, fast, and reliable How JUnit Works • 1. Write your Java class (e.g., Calculator.java) • 2. Create a test class (e.g., CalculatorTest.java) • 3. Write methods using @Test • 4. JUnit runs tests and checks output • 5. Shows result with green/red bar JUnit Annotations • @Test – Marks a method as a test • @Before – Runs before every test • @After – Runs after every test • @BeforeClass – Runs once before all tests • @AfterClass – Runs once after all tests • @Ignore – Skips a test JUnit Assertions • - assertEquals(a, b): checks if a == b • - assertTrue(condition) • - assertFalse(condition) • - assertNull(obj), assertNotNull(obj) • - assertSame(a, b), assertNotSame(a, b) • - fail(): forces test to fail JUnit Calculator Example • public int add(int a, int b) { return a + b; } • @Test • public void testAdd() { • assertEquals(5, calc.add(2,3)); • } Parameterized Tests • @ParameterizedTest • @CsvSource({"2,3,5", "7,3,10"}) • void testAdd(int a, int b, int expected) { • assertEquals(expected, calc.add(a, b)); • } Repeated Tests • @RepeatedTest(3) • void repeatAdditionTest() { • assertEquals(5, calc.add(2, 3)); • } Test Suite in JUnit • @RunWith(Suite.class) • @Suite.SuiteClasses({Test1.class, Test2.class}) • public class AllTestsSuite {} JUnit in Eclipse – Setup • 1. Create Java Project • 2. Download JUnit JARs • 3. Add to Build Path > Libraries • 4. Create test classes • 5. Run as > JUnit Test Login Test Example • LoginService login = new LoginService(); • @Test • public void testLogin() { • assertTrue(login.login("admin", "admin123")); • } Summary – What to Remember • - JUnit tests your Java code automatically • - Annotations like @Test and assertions like assertEquals help write tests • - Test Suites and Parameterized Tests make testing easier • - Common in real-world software development