Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy mock method with parameters

I tried to mock some maven classes.

I know I can mock an Interface or a class with maps

def projectMock = [ getBasedir: { new File("") } ] as MavenProject

But how do I mock a method that receives parameters?

I have tried to use "MockFor" for this:

def artifactFactoryMockContext = new MockFor(ArtifactFactory)
artifactFactoryMockContext.demand.createArtifact(1) {groupId, artifactId, version, classifier, type -> artifact }
def artifactFactory = artifactFactoryMockContext.proxyInstance()

But I get an UnsupportedOperationException. What am I doing wrong here?

like image 785
Marc von Renteln Avatar asked Feb 21 '23 03:02

Marc von Renteln


1 Answers

Long as you're ok using Groovy Map coercion for mocking instead of a framework, this kind of thing will work for you:

def fooMock = [ bar: { baz, thing -> 42 } ] as Foo

Now fooMock.bar("arg1", "arg2") will return 42.

like image 179
Eric Wendelin Avatar answered Mar 03 '23 15:03

Eric Wendelin