Groovy Unit Testing By Franz See [email_address] http://twitter.com/franz_see
Agenda I. Review Basics A. Parts B. Organization II. Groovy Test
Review Basics :: Parts @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class Move_Validation extends GivenGameBoard { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1() { } @Test( expected = IllegalMoveException.class ) void givenXIsAbove3() { } ... } static class Move_Marking extends GivenGameBoard { @Test void givenValidMove_shouldMarkLocation() { } @Test void givenValidMoves_shouldMarkLocations() { } } ... }
Review Basics :: Parts @Test void givenValidMove_shouldMarkLocation() { int x = 1, y = 3 gameBoard.move(x, y) assertEquals "Should have been marked 'X' after the move.",  X, gameBoard.getCells()[y-1][x-1] }
Review Basics :: Parts @Test void givenValidMove_shouldMarkLocation() { int x = 1, y = 3 assertThat "[GUARD] Makes sure that it is not 'X' ' +  'before the move.",  gameBoard.getCells()[y-1][x-1], not(equalTo(X)) gameBoard.move(x, y) assertEquals "Should have been marked 'X' after the move.",  X, gameBoard.getCells()[y-1][x-1] }
Review Basics :: Organization A. Test by Class B. Test by Feature C. Test by Fixture
Review Basics :: Organization :: by Class @RunWith(Suite.class) class GameBoardTests { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1_whenMove_then _thenComplain ()  {…} @Test( expected = IllegalMoveException.class ) void givenXIsAbove3_whenMove_thenComplain()  {…} @Test void givenValidMove_whenMove_shouldMarkLocation()  {…} @Test void givenValidMoves_ whennMove_ shouldMarkLocations() {…} }
Review Basics :: Organization :: by Feature @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class Move { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1_then_thenComplain() {…} @Test( expected = IllegalMoveException.class ) void givenXIsAbove3_thenComplain() {…} @Test void givenValidMove_shouldMarkLocation() {…} @Test void givenValidMoves_shouldMarkLocations() {…} } }
Review Basics :: Organization :: by Fixture @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class GivenXIsBelow1 { @Test( expected = IllegalMoveException.class ) void thenComplain() {…} } static class GivenXIsAbove3 { @Test( expected = IllegalMoveException.class ) void thenComplain() {…} } static class GivenValidMove { @Test void shouldMarkLocation() {…} @Test void shouldMarkLocations() {…} } }
Groovy Test (see code)
Thank you. Questions?
References Xunit Test Patterns

Testing in-groovy

  • 1.
    Groovy Unit TestingBy Franz See [email_address] http://twitter.com/franz_see
  • 2.
    Agenda I. ReviewBasics A. Parts B. Organization II. Groovy Test
  • 3.
    Review Basics ::Parts @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class Move_Validation extends GivenGameBoard { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1() { } @Test( expected = IllegalMoveException.class ) void givenXIsAbove3() { } ... } static class Move_Marking extends GivenGameBoard { @Test void givenValidMove_shouldMarkLocation() { } @Test void givenValidMoves_shouldMarkLocations() { } } ... }
  • 4.
    Review Basics ::Parts @Test void givenValidMove_shouldMarkLocation() { int x = 1, y = 3 gameBoard.move(x, y) assertEquals "Should have been marked 'X' after the move.", X, gameBoard.getCells()[y-1][x-1] }
  • 5.
    Review Basics ::Parts @Test void givenValidMove_shouldMarkLocation() { int x = 1, y = 3 assertThat "[GUARD] Makes sure that it is not 'X' ' + 'before the move.", gameBoard.getCells()[y-1][x-1], not(equalTo(X)) gameBoard.move(x, y) assertEquals "Should have been marked 'X' after the move.", X, gameBoard.getCells()[y-1][x-1] }
  • 6.
    Review Basics ::Organization A. Test by Class B. Test by Feature C. Test by Fixture
  • 7.
    Review Basics ::Organization :: by Class @RunWith(Suite.class) class GameBoardTests { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1_whenMove_then _thenComplain () {…} @Test( expected = IllegalMoveException.class ) void givenXIsAbove3_whenMove_thenComplain() {…} @Test void givenValidMove_whenMove_shouldMarkLocation() {…} @Test void givenValidMoves_ whennMove_ shouldMarkLocations() {…} }
  • 8.
    Review Basics ::Organization :: by Feature @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class Move { @Test( expected = IllegalMoveException.class ) void givenXIsBelow1_then_thenComplain() {…} @Test( expected = IllegalMoveException.class ) void givenXIsAbove3_thenComplain() {…} @Test void givenValidMove_shouldMarkLocation() {…} @Test void givenValidMoves_shouldMarkLocations() {…} } }
  • 9.
    Review Basics ::Organization :: by Fixture @RunWith(Suite.class) @Suite.SuiteClasses([..]) class GameBoardTests { static class GivenXIsBelow1 { @Test( expected = IllegalMoveException.class ) void thenComplain() {…} } static class GivenXIsAbove3 { @Test( expected = IllegalMoveException.class ) void thenComplain() {…} } static class GivenValidMove { @Test void shouldMarkLocation() {…} @Test void shouldMarkLocations() {…} } }
  • 10.
  • 11.
  • 12.