Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add example of passing arguments to a constructor via a proxyInstance call.

...

Code Block
import grails.test.*
import groovy.mock.interceptor.*

class MockForTests extends GroovyTestCase {

   void test_mock_single_method_and_use_as_category()     {
        def mock = new MockFor(MockForTestsClass)
        mock.demand.amethod { "from mock"}
        mock.use {
        	assertEquals "from mock", new MockForTestsClass().amethod()
        }
    }

    void test_mock_single_static_method_and_use_as_category()     {
        def mock = new MockFor(MockForTestsClass)
        mock.demand.astaticmethod { "from static mock"}
        mock.use {
        	assertEquals "from static mock", MockForTestsClass.astaticmethod()
        }
    }

    void test_mock_single_method_andwith_use_proxyparameters()     {
        def mock = new MockFor(MockForTestsClass)
        mock.demand.amethodamethodwithparameters { a -> "from mock with proxy"parameters ${a}" }

        def proxy = mock.proxyInstance()use {
       assertEquals "from mock with proxyparameters 123",
proxy.amethod()         mock.verify proxy
    }

    void test_stub_single_method()
    {
        def mock = new StubFor(MockForTestsClassnew MockForTestsClass().amethodwithparameters(123)
    }
   mock.demand.amethod { "from stub" }

  /*
   * mock.useThis {is a special case problem related to a bug. 	assertEqualsThis "from stub", new MockForTestsClass().amethod()proves the work around.
   */
    }
    }

    void test_stubmocking_singlea_method_andcalled_expectfind_no_errorsis_a_special_case()     {
        def mock = new StubForMockFor(MockForTestsClass)
        mock.demand.amethodfind(1) { "notcardinality called1" }
        mock.use {
      assertEquals  	new"cardinality 1", MockForTestsClass.find()
        }
    }

    void test_mock_single_method_withand_parametersuse_proxy()     {
        def mock = new MockFor(MockForTestsClass)
        mock.demand.amethodwithparametersamethod { a -> "from mock with parameters ${a}" proxy"}

    def proxy =  mock.useproxyInstance()
{         	assertEquals "from mock with parameters 123proxy",         		new MockForTestsClass().amethodwithparameters(123proxy.amethod()
    mock.verify proxy
  }

   }


    /*
     * This is a special case problem related to a bug. This proves the work around.
     */
    void test_mockingcreate_aproxy_methodinstance_calledwith_find_is_a_special_caseconstructor_arguments()     {
        def mock = new MockFor(MockForTestsClassMockForTestClassWithConstructorArgs)
        mock.demand.find(1)amethod { "cardinality 1" from mock with proxy"}

    def proxy = mock.use {
proxyInstance(["value1", "value2"]as Object[])
    assertEquals "from mock with    assertEquals "cardinality 1"proxy", MockForTestsClassproxy.findamethod()
    mock.verify proxy
  }

   }

    void test_mocking_a_constructor_call() {
        def mock = new MockFor(MockForTestsClass, true)
        mock.demand.with {
            MockForTestsClass() {  throw new Exception("YES IT WORKED") }
        }

       mock.use {
            shouldFail Exception, { new MockForTestsClass() }
        }
    }

      /*
     * Constructor mocking requires that an instance of a class is returned
     * and that instance can be cast to the mocked type.
     */
    void test_mock_a_constructor_that_gets_used() {
        def mock = new MockFor(MockForTestsClass, true)
        mock.demand.with {
              MockForTestsClass() { new Object() as GroovyObject }
        }

            mock.use {  new MockForTestsClass() }
    }

     /*
     * To demand more interactions with the object beyond the
      * constructor the mocked constructor most return the proxy
     * instance instead.
     *
      * NOTE: MockFor constructs an instance of the real class when
     * creating the proxy instance. This will require that the
     * required parameters are passed in to the proxyInstance
     * call for forwarding to the real constructor.
     */
    void test_mock_a_constructor_that_returns_a_proxy_instance() {
        def mock = new MockFor(MockForTestsClass, true)

            def theProxyInstance = mock.proxyInstance()

            mock.demand.with {
               MockForTestsClass() { theProxyInstance }
            amethod() { }
        }

            mock.use {
              def testClass= new MockForTestsClass()
            testClass.amethod()
        }
    }
}

class MockForTestsClass {

   void amethod()     {
        println "original method"
    }

    static void astaticmethod() {
   { println "original static method"
  }

  void amethodwithparameters(message) {
    println "original static method" with parameters"
  }
}

class MockForTestClassWithConstructorArgs extends void amethodwithparameters(message)MockForTestsClass {
  def arg1
  def arg2
  MockForTestClassWithConstructorArgs(arg1, arg2) {
    this.arg1 = arg1
 println "original method with parameters"
this.arg2 = arg2
   }
}