SWEA 2023 Weekplan 5

The learning goals for Week 39 are:

Mon: State Pattern. Wed: Test Doubles. Abstract Factory.

Literature:

Slides:

Notes for this weekplan:

Due to illness, both lectures were unfortunately cancelled. They were replace by last years panopto videos
  1. State
  2. Test doubles + Abstract Factory

Additional exercises:

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-2022.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.)

Legend: The typography bold, normal, (brackets), above indicate my perception of how important the exercises are from high to optional. However, solve the mandatory project first!