How can I use the Groovy mock.interceptor package to model an object constructor?

In my attempt to mock an object in Groovy using a package mock.interceptor:

def mock = new MockFor(TheClass);
mock.demand.theMethod{ "return" }
mock.use {
    def underTest = new TheClass()
    println underTest.theMethod()
}

The problem is creating TheClass()in the block use{, it uses the actual constructor, which in this case I would prefer not to use it. How do I instantiate this class so that I can test the method I care about theMethodwithout using a constructor?

Using EasyMock / CE, mocks can be done without using a constructor, but curious how to achieve this in Groovy.

+3
source share
2 answers

GMock, , "", , , , , .

.

 def mockFile = mock(File, constructor('/a/path/file.txt'))

"" groovy, , , , , , , , .

, , . , - , Factory, .

+1

interceptConstruction MockFor, . MockFor.

+1

Source: https://habr.com/ru/post/1706336/


All Articles