Jeff Storey
Jeff Storey

Reputation: 57192

groovy mocks with abstract methods

I have a Java object called Parameter and I'm trying to mock it using groovy. Parameter is an abstract class with 1 abstract method. It also has a non-abstract method called getName(). I'm trying to mock it as follows in Groovy:

 def p1 = [name:{"p1Name"}] as Parameter

But I get a runtime error because I don't implement the abstract method. If I'm trying to create a mock, why would I need to implement the abstract method?

thanks, Jeff

Upvotes: 2

Views: 2055

Answers (1)

John Wagenleitner
John Wagenleitner

Reputation: 11035

By mocking using the map you are creating an instance of type Parameter and so it must implement any abstract methods of the Parameter class.

abstract class Parameter {
  abstract String getOtherName() 
  String getName() { return "test" }
}

def p1 = [name:{"p1Name"}, getOtherName:{""}] as Parameter

Upvotes: 3

Related Questions