Code-Alongs
Overview
Join us for interactive coding sessions where you'll build real-world applications applying the design patterns and testing techniques covered in this sprint. These hands-on exercises will help reinforce your understanding of dependency injection, test-driven development, and related concepts.
Remote Controller: Dependency Injection
In this code-along, you'll build a TV remote control application using dependency injection principles to create a flexible and testable solution.
What You'll Learn
- How to identify tightly-coupled code and refactor it
- How to implement dependency injection manually
- How to design interfaces that facilitate testing
- How to use constructor injection to improve testability
Code Preview
// Before dependency injection (tightly coupled)
public class RemoteControl {
private final Television tv = new SonyTelevision();
public void powerOn() {
tv.turnOn();
}
public void powerOff() {
tv.turnOff();
}
}
// After dependency injection (loosely coupled)
public class RemoteControl {
private final Device device;
public RemoteControl(Device device) {
this.device = device;
}
public void powerOn() {
device.turnOn();
}
public void powerOff() {
device.turnOff();
}
}
Test-Driven Development: To-Do List Application
In this code-along, you'll develop a to-do list application using test-driven development. You'll write tests first, then implement the functionality to make those tests pass.
What You'll Learn
- How to apply the red-green-refactor cycle
- How to write effective unit tests
- How to build functionality incrementally with TDD
- How to use JUnit effectively for test-driven development
Code Preview
// Task 1: Write a failing test first (Red)
@Test
void shouldAddTaskToList() {
// Arrange
TodoList todoList = new TodoList();
Task task = new Task("Complete assignment");
// Act
todoList.addTask(task);
// Assert
assertEquals(1, todoList.getTasks().size());
assertTrue(todoList.getTasks().contains(task));
}
// Task 2: Implement the functionality (Green)
public class TodoList {
private final List tasks = new ArrayList<>();
public void addTask(Task task) {
tasks.add(task);
}
public List getTasks() {
return Collections.unmodifiableList(tasks);
}
}
Mockito: Board Game Service
In this code-along, you'll build a board game service and use Mockito to test its functionality in isolation from its dependencies.
What You'll Learn
- How to use Mockito to create mock objects
- How to stub methods to return test data
- How to verify interactions with dependencies
- How to use argument captors and matchers
Code Preview
@Test
void shouldRetrieveGameAndUpdateStatistics() {
// Arrange
GameRepository mockRepository = mock(GameRepository.class);
StatisticsService mockStats = mock(StatisticsService.class);
Game testGame = new Game("Chess", "Strategy");
when(mockRepository.findByName("Chess")).thenReturn(testGame);
GameService service = new GameService(mockRepository, mockStats);
// Act
Game result = service.getGameDetails("Chess");
// Assert
assertEquals("Chess", result.getName());
assertEquals("Strategy", result.getCategory());
// Verify interactions
verify(mockRepository).findByName("Chess");
verify(mockStats).recordGameView("Chess");
}