2

I am not able to figure out partial mocking in groovy using gmock. I have the following code:

class Foo {
   Integer val
   Foo() {
      this.val = 4;
   }

   Integer printHello() {
      return getValue()
   }

   Integer getValue() {
      return val+1;
   }
}

and the testcase:

class FooTester {
   @Test
   void test() {
      def lol = new Foo(4)
      def mocker = mock(lol)
      mocker.getValue().returns(5)

      play {
         assertEquals(5, lol.printHello())
      }
   }
}

I am referring to the documentation here. Assertion is failing with java.lang.AssertionError: Expectation not matched on verify:

What may the problem?

Opal
  • 81,889
  • 28
  • 189
  • 210
thePoly_glot
  • 135
  • 1
  • 2
  • 11
  • What happens if you mock as `mocker.value.returns(5)`, using property syntax instead of method syntax? – BalRog Oct 17 '14 at 00:04

1 Answers1

0

After quick check of documentation, I think this should work:

class FooTester {
   @Test
   void test() {
      def lol = new Foo(4)
      mock(lol).value.returns(5)

      play {
         assertEquals(5, lol.printHello())
      }
   }
}
Todd W Crone
  • 1,185
  • 8
  • 23