Marc von Renteln
Marc von Renteln

Reputation: 1257

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?

Upvotes: 4

Views: 6310

Answers (1)

Eric Wendelin
Eric Wendelin

Reputation: 44399

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.

Upvotes: 8

Related Questions