0% found this document useful (0 votes)
8 views14 pages

JUnit Complete Training Presentation

JUnit is a widely used Java testing framework for writing and running automated unit tests, ensuring code correctness by comparing actual results with expected outcomes. It supports various testing methodologies, including Test-Driven Development (TDD), and utilizes annotations and assertions to define test behavior and validate results. The guide covers basic test examples, parameterized tests, and Eclipse setup for JUnit, highlighting its importance in software development and interviews.
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
8 views14 pages

JUnit Complete Training Presentation

JUnit is a widely used Java testing framework for writing and running automated unit tests, ensuring code correctness by comparing actual results with expected outcomes. It supports various testing methodologies, including Test-Driven Development (TDD), and utilizes annotations and assertions to define test behavior and validate results. The guide covers basic test examples, parameterized tests, and Eclipse setup for JUnit, highlighting its importance in software development and interviews.
Copyright
© © All Rights Reserved
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/ 14

JUnit Complete Guide

Learn, Understand, and Explain JUnit


Confidently
What is JUnit?
• JUnit is a Java testing framework used to write
and run automated unit tests. It checks if small
parts of your code (called units) work correctly
by comparing actual results with expected
results. It's the most commonly used testing
framework in Java development.
Why Use JUnit?
• - Automatically checks if code is working
• - Saves time and avoids manual errors
• - Great for Agile and DevOps teams
• - Supports Test-Driven Development (TDD)
• - Used with build tools like Maven, Jenkins
How JUnit Works
• 1. Write the class to be tested (e.g.,
Calculator.java)
• 2. Write a separate test class using @Test
methods
• 3. JUnit compares expected vs actual output
• 4. Shows GREEN bar if tests pass, RED bar if
they fail
JUnit Annotations
• @Test – Marks a method as a test
• @Before – Runs before each test (e.g., setup)
• @After – Runs after each test (e.g., cleanup)
• @BeforeClass – Runs once before all tests
• @AfterClass – Runs once after all tests
• @Ignore – Skips a test
JUnit Assertions
• - assertEquals(expected, actual): checks if two
values are equal
• - assertTrue(condition), assertFalse(condition)
• - assertNull(obj), assertNotNull(obj)
• - assertSame(obj1, obj2), assertNotSame(obj1,
obj2)
• - fail(): manually fail a test case
Basic Test Example
• public class Calculator {
• public int add(int a, int b) {
• return a + b;
• }
• }

• public class CalculatorTest {


• @Test
• public void testAdd() {
Test Case: Divide with Exception
• @Test(expected =
IllegalArgumentException.class)
• public void testDivideByZero() {
• Calculator c = new Calculator();
• c.divide(10, 0); // Should throw exception
• }
Parameterized Test
• @ParameterizedTest
• @CsvSource({"2, 3, 5", "7, 3, 10"})
• void testAdd(int a, int b, int expected) {
• assertEquals(expected, calc.add(a, b));
• }
Repeated Test
• @RepeatedTest(3)
• void repeatAddTest() {
• assertEquals(5, calc.add(2, 3));
• }
Test Suite
• @RunWith(Suite.class)
• @Suite.SuiteClasses({ AddTest.class,
MultiplyTest.class })
• public class AllTests {}
Eclipse Setup
• 1. Create Java Project
• 2. Download JUnit JAR files from junit.org
• 3. Right-click Project > Build Path > Add
External JARs
• 4. Create class and test case under src folder
• 5. Run test and see GREEN/RED bar
Login Test Example
• public class LoginService {
• public boolean login(String u, String p) {
• return u.equals("admin") &&
p.equals("admin123");
• }
• }

• @Test
• public void testLogin() {
Final Summary
• - JUnit tests your Java code automatically
• - Annotations define test flow (@Test,
@Before, etc.)
• - Assertions compare expected vs actual
• - Suites and parameterized tests make it
powerful
• - Used in real software projects and interviews

You might also like