Skip to content

Exercise 012: Remove Dependence on Class State

🔗 Related Smell: 012 - Dependence on Class State

Purpose

  • Learn to identify the Dependence on Class State smell.
  • Understand how complexity increases from having shared data on multiple places instead of local parameters.

Your Task

The step implementation has different fields i.e class state, which can be grouped by system under test, input data and results. We want to get rid of the input data and replace it with local parameters and variables.

Solution

Hint A
  • Identify the three groups of class state in your step implementation:
    • system under test
    • input data
    • results
Hint B
  • The input data state is storing information about the customers to be created: first name, last name, ...
  • Remove the first name and last name and analyse the compiler errors.
  • What else can we remove now?
Hint C
  • Remove the first name and last name.
  • Remove all Givensteps in your step implementation and in the Customer.feature file dealing with first name and last name.
  • How can we deal with the missing data in the When and Then steps that instead?
Hint D
  • Remove the first name and last name.
  • Remove all Givensteps in your step implementation and in the Customer.feature file dealing with first name and last name.
  • Add the parameters firstName and lastName to 'When the customer is created'.
  • Remove Then the customer can be found because there is already a expression with the parameters.
  • Change the Customer.feature file accordingly and pass the parameters via the steps.
  • Which class state can be remove now?
Step by Step Walkthrough
  • Remove the first name and last name.
  • Remove all Givensteps in your step implementation and in the Customer.feature file dealing with first name and last name.
  • Add the parameters firstName and lastName to 'When the customer is created'.
  • Remove Then the customer can be found because there is already a expression with the parameters.
  • Change the Customer.feature file accordingly and pass the parameters via the steps.
  • Remove the state for handling a second customer in the same way as previously the first name and last name.
  • Pass parameters to the expression for the second customer.
  • Merge the expression and delete the unnecessary functions for example: 'Then the customer {} {} can be found' and 'Then the second customer {} {} can be found'