Legacy code example

Software concepts are often easiest to explain through code, and this one is no exception. We have seen and worked with the Tic-Tac-Toe application (see Chapter 3, Red-Green-Refactor – From Failure Through Success until Perfection). The following code performs position validation:

public class TicTacToe { 
  public void validatePosition(int x, int y) { 
    if (x < 1 || x > 3) { 
      throw new RuntimeException("X is outside board"); 
    } 
    if (y < 1 || y > 3) { 
      throw new RuntimeException("Y is outside board"); 
    } 
  } 
} 

The specification that corresponds with this code is as follows:

public class TicTacToeSpec {   @Rule 
  public ExpectedException exception = 
      ExpectedException.none(); 
   private TicTacToe ticTacToe; @Before public final void before() ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.