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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With