SWEA 2025 Weekplan 5

The learning goals for Week 39 are:

Mon: Test Doubles. Wed: Abstract Factory. Mandatory reflections. Debugging.

Literature:

Slides:

Notes for this weekplan:

We start Monday at 9.15 where I will talk about Test doubles.

Wednesday I will focus on Abstract Factory and reflections on the mandatory. The Debugging slides are if time permits (but if you struggle with finding your bugs, have a look for advice in the slide set.)

Kata

Spend the first 15-20 minutes of the TØ/Lab class in plenum discussing...

Consider this HotStone endOfTurn() implementation, and find examples of Clean Code principles that are violated.

Time permitting, suggest ways to refactor the code to become more clean.

Optional Exercises:

These are optional exercises. Be sure to solve the mandatory project first, and only if you have a lot of spare time, try having a look at the exercises below.

11.4 11.5 12.5 13.6

Exam Rehearsal

Rehearse a variability exam situation using an exercise in the example exam question set: demo-question-final-2023.pdf.

Next, do a 10 minute presentation at the whiteboard in front of your team.

The team provides constructive feedback on the presentation: Is your presentation clear and understandable, do you discuss the concepts and terms correctly, is you Java example code correct, etc. Swap and ensure all in the team gets a chance to rehearse the situation.

Mock libraries: Mockito

Mock objects ("mocks") are autogenerated test doubles and may speed up TDD substantially in case you have a lot of indirect input or output. One library is Mockito, see Vogel's tutorial.

As an example (described in FRS §12.6.2), the code below shows how a chemical plant temperature control system (Unit Under Test is "plantMonitor.regulateTemperature()") can be tested using Mockito mock objects for the temperature sensor and the cooling system:

public class TestTemperatureRegulation {
  private PlantMonitor plantMonitor;
  private TemperatureSensor temperatureSensor;
  private CoolingSystem coolingSystem;

  @BeforeEach
  public void setup() {
    temperatureSensor = mock(TemperatureSensor.class);
    coolingSystem = mock(CoolingSystem.class);
    plantMonitor = new StandardPlantMonitor(temperatureSensor, 
                                            coolingSystem);
  }

  @Test
  public void shouldTurnOnCoolingAbove67degrees() {
    // Given a temperature sensor which returns 67.2
    when(temperatureSensor.readTemperature()).thenReturn(67.2);
    // When the monitor needs to regulate the temperature
    plantMonitor.regulateTemperature();
    // Then cooling is commanded to turn on cooling
    verify(coolingSystem).turnCoolingOn();
  }
}        

Download the mockito zip file to see the code and test it. (It is equivalent to the contents in the www.baerbak.com FRS source code zip file.)