Grails
  1. Grails
  2. GRAILS-8778

Mocking validate() method of domain object broken in Grails 2.0.1

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0.1
    • Fix Version/s: None
    • Component/s: Testing
    • Labels:
    • Environment:
      Windows XP, Oracle java version "1.6.0_30"
    • Testcase included:
      yes

      Description

      Mocking validate() method as no effect. Calls to validate() always return true. It worked in Grails 2.0.0.

      The test : it should be green but it isn't

      @TestFor(BookController)
      class BookControllerTests {

      Book book

      @Test
      void testValid() {
      def controlBook = mockFor(Book)
      controlBook.demand.static.get(0..1)

      { Integer id -> id == 1 ? book : null}

      controlBook.demand.validate(0..1) {-> Boolean.FALSE}
      book = controlBook.createMock()

      book.title = 'Title'
      book.author = 'Author'

      controller.test()

      assert response.text == 'Book invalid'
      }
      }

      The controller :

      class BookController {

      static scaffold = true

      def test() {

      def book = Book.get(1)

      if(book.validate())

      { render "Book valid" return }

      render "Book invalid"

      }
      }

      The domain class :

      class Book {

      String title
      String author

      static constraints =

      { title (nullable:false, blank: false) author (nullable:false, blank: false) }

      }

        Activity

        Hide
        Sebastien Errien added a comment -

        If validate methode is mocked before static get methode :

        controlBook.demand.validate {-> Boolean.FALSE}
        controlBook.demand.static.get(0..1)

        { Integer id -> id == 1 ? book : null}

        Validate return false, but test fail with message :

        junit.framework.AssertionFailedError: No more calls to 'validate' expected at this point. End of demands.

        at grails.test.MockClosureProxy.doBeforeCall(MockClosureProxy.java:66)
        at grails.test.AbstractClosureProxy.call(AbstractClosureProxy.java:74)
        at grails.test.GrailsMock$_createMock_closure1.doCall(GrailsMock.groovy:125)
        at tests.BookController.test(BookController.groovy:11)
        at tests.BookControllerTests.testValid(BookControllerTests.groovy:33)

        Show
        Sebastien Errien added a comment - If validate methode is mocked before static get methode : controlBook.demand.validate {-> Boolean.FALSE} controlBook.demand.static.get(0..1) { Integer id -> id == 1 ? book : null} Validate return false, but test fail with message : junit.framework.AssertionFailedError: No more calls to 'validate' expected at this point. End of demands. at grails.test.MockClosureProxy.doBeforeCall(MockClosureProxy.java:66) at grails.test.AbstractClosureProxy.call(AbstractClosureProxy.java:74) at grails.test.GrailsMock$_createMock_closure1.doCall(GrailsMock.groovy:125) at tests.BookController.test(BookController.groovy:11) at tests.BookControllerTests.testValid(BookControllerTests.groovy:33)

          People

          • Assignee:
            Unassigned
            Reporter:
            Sebastien Errien
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development