Skip to content

mkdir-andy/java-tdd-practice-codebar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Calculator TDD Practice

A hands-on Test-Driven Development (TDD) exercise for practicing JUnit 5 and Mockito with a simple calculator application.

Project Structure

src/
├── main/java/com/codebar/calculator/
│   ├── Calculator.java           # Basic calculator operations
│   └── CalculatorService.java    # Service layer using Calculator
└── test/java/com/codebar/calculator/
    ├── CalculatorTest.java        # Unit tests for Calculator
    └── CalculatorServiceTest.java # Integration tests with Mockito

Prerequisites

  • Java 17 or higher
  • Maven 3.6+

Getting Started

1. Build the project

mvn clean compile

2. Run the tests

mvn test

3. Run tests with coverage (optional)

mvn test jacoco:report

TDD Workflow

Follow the Red-Green-Refactor cycle:

Red Phase

  1. Write a failing test first
  2. Run the test and see it fail
  3. Ensure the failure is for the right reason

Green Phase

  1. Write the minimum code to make the test pass
  2. Run the test and see it pass
  3. Don't worry about code quality yet

Refactor Phase

  1. Improve the code while keeping tests green
  2. Remove duplication
  3. Improve naming and structure
  4. Run tests frequently to ensure nothing breaks

Exercise Goals

Part 1: Basic Calculator (CalculatorTest.java)

Implement and test basic calculator operations:

  • add(int a, int b) - Add two numbers
  • subtract(int a, int b) - Subtract two numbers
  • multiply(int a, int b) - Multiply two numbers
  • divide(int a, int b) - Divide two numbers

Test cases to consider:

  • Positive numbers
  • Negative numbers
  • Zero
  • Edge cases (division by zero, overflow, etc.)

Part 2: Calculator Service with Mocking (CalculatorServiceTest.java)

Practice Mockito by testing the CalculatorService:

  • calculateTotal(int... numbers) - Sum all numbers
  • calculateAverage(int... numbers) - Calculate average
  • multiplyAll(int... numbers) - Multiply all numbers

Learn to:

  • Mock dependencies using @Mock
  • Inject mocks using @InjectMocks
  • Stub method behavior with when().thenReturn()
  • Verify method calls with verify()
  • Test interaction between objects

Key JUnit 5 Annotations

  • @Test - Marks a test method
  • @BeforeEach - Runs before each test
  • @DisplayName - Custom test name for reports
  • @ExtendWith(MockitoExtension.class) - Enables Mockito

Key Mockito Annotations

  • @Mock - Creates a mock instance
  • @InjectMocks - Creates instance and injects mocks
  • when().thenReturn() - Stubs method behavior
  • verify() - Verifies method was called
  • times() - Specifies number of invocations

Common Assertions

assertEquals(expected, actual)
assertTrue(condition)
assertFalse(condition)
assertThrows(Exception.class, () -> code)
assertNotNull(object)

Tips

  1. Write one test at a time
  2. Start with the simplest test case
  3. Make small incremental changes
  4. Run tests frequently
  5. Refactor only when tests are green
  6. Test behavior, not implementation
  7. Use descriptive test names

Resources

License

This is a practice project for educational purposes.

About

TDD in Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages