Groovy method layout with parameters

I tried to make fun of some maven classes.

I know that I can mock an interface or class with maps

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

But how do I make fun of a method that receives parameters?

I tried using "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?

+4
source share
1 answer

Until you use Groovy Forcing a card for ridicule instead of a framework, this will work for you:

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

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

+8
source

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


All Articles