How to use closure to capture the invocation by specific type and ignore others? - groovy

I have a mock with a method that can accept all children of an abstract type.In a test,this method may be invoked by different children multiple times,and I want to check that it is invoked by specific type once like this:
1*mockObject.method(_) {SpecificType ct->
//check the ct's field value
}
the problem is that the closure always capture the first invocation due to:
mockObjectt.method(_)
can't distinguish different children types,so it match all invocations,then I tried another way like this:
1*mockObject.method({it instanceof SpecificType}) {SpecificType ct->
//check the ct's field value
}
but the problem of this way is that:
{SpecificType ct->
//Here,any check will return true,
//I think the checkings in here are ignored
}
is ignored always(I think I misused the groovy's closure here)
So question is:
Is there elegant way to capture the invocation by specific child type with specific values and ignore others?

What about: 1*mockObject.method({it instanceof SpecificType && it.field == fieldValue }) ?

Take a look at this sample code:
import spock.lang.Specification
class SimpleTestSpec extends Specification {
private static class A { }
private static class B extends A { }
private static class C extends B { }
private static class D extends A { }
private static class TestClass {
String foo(A value) {
return "A: ${value}"
}
String foo(B value) {
return "B: ${value}"
}
String foo(C value) {
return "C: ${value}"
}
String foo(D value) {
return "D: ${value}"
}
}
private static class ComponentClass {
private final TestClass testClass
ComponentClass(TestClass testClass) {
this.testClass = testClass
}
String foo(A value) {
return testClass.foo(value)
}
}
TestClass testClassMock = Mock(TestClass)
ComponentClass componentClass = new ComponentClass(testClassMock)
def setup() {
testClassMock.foo(_ as D) >> "DDD"
testClassMock.foo(_ as C) >> "CCC"
testClassMock.foo(_ as B) >> "BBB"
testClassMock.foo(_ as A) >> "AAA"
}
def "should use mocked DDD result"() {
when:
String result = testClassMock.foo(new D())
then:
result == "DDD"
}
def "should use mocked CCC result"() {
when:
String result = testClassMock.foo(new C())
then:
result == "CCC"
}
def "should use mocked BBB result"() {
when:
String result = testClassMock.foo(new B())
then:
result == "BBB"
}
def "should use mocked AAA result"() {
when:
String result = testClassMock.foo(new A())
then:
result == "AAA"
}
def "should record invocation based on dynamic type"() {
when:
componentClass.foo(new C())
then:
1 * testClassMock.foo(_ as C)
}
def "should return result associated with mocked invocation in runtime"() {
when:
String result = componentClass.foo(new D())
then:
result == "DDD"
}
}
Gist file: https://gist.github.com/wololock/c59151b67d4c9b0c0c8e
You can specify expected type of an argument using as casting operator. But there is one tricky part - you need to stub class behavior in particular order: from most specific to most general. Try to mix the order in setup() and then you will see that tests will start failing.
You can also make use of Groovy's dynamic method invocation - notice how ComponentClass behaves with injectest TestClass instance which was previously stubbed with expected behavior. You can also count the number of invocations associated with specific type even if external call was made through the most general type method.
I hope it will help you resolve your problem. Best!

Related

Looping through class members and update member value in groovy

I have a question regarding loop through class members and update member value of the object in groovy:
class Test {
String a
String b
Test(String a, String b) {
this.a = a
this.b = b
}
String toString() {
return "a is " + a + " b is " + b
}
}
And I want to loop through the object member and update the value of the member:
class Testing {
static void main(String[] args) {
Test test = new Test("hello", "world")
test.properties.findAll {
it.value.toString.equals('hello')
}.each {
it.setValue("new value")
}
}
}
I try to change the value of "hello" to "new value", looks like it can find the member contains the "hello", but the value the same after it.setvalue(), how to change the value of the member in the object in correct way?
Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.
Consider the following example:
class Testing {
static void main(String[] args) {
Test test = new Test("hello", "world")
test.properties.findAll {
it.value == 'hello'
}.each { field, _ ->
test.setProperty(field as String, "new value")
}
println test
}
}
Output:
a is new value b is world

What is the static version of propertyMissing method in Groovy?

ok - tried looking /reading and not sure i have an answer to this.
I have a Utility class which wraps a static ConcurrentLinkedQueue internally.
The utility class itself adds some static methods - i dont expect to call new to create an instance of the Utility.
I want to intercept the getProperty calls the utility class - and implement these internally in the class definition
I can achieve this by adding the following to the utility classes metaclass, before i use it
UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "accessed prop called $name"}
println UnitOfMeasure.'Each'
however what i want to do is declare the interception in the class definition itself. i tried this in the class definition - but it never seems to get called
static def propertyMissing (receiver, String propName) {
println "prop $propName, saught"
}
i also tried
static def getProperty (String prop) { println "accessed $prop"}
but this isnt called either.
So other than adding to metaClass in my code/script before i use, how can declare the in the utility class that want to capture property accesses
the actual class i have looks like this at present
class UnitOfMeasure {
static ConcurrentLinkedQueue UoMList = new ConcurrentLinkedQueue(["Each", "Per Month", "Days", "Months", "Years", "Hours", "Minutes", "Seconds" ])
String uom
UnitOfMeasure () {
if (!UoMList.contains(this) )
UoMList << this
}
static list () {
UoMList.toArray()
}
static getAt (index) {
def value = null
if (index in 0..(UoMList.size() -1))
value = UoMList[index]
else if (index instanceof String) {
Closure matchClosure = {it.toUpperCase().contains(index.toUpperCase())}
def position = UoMList.findIndexOf (matchClosure)
if (position != -1)
value = UoMList[position]
}
value
}
static def propertyMissing (receiver, String propName) {
println "prop $propName, saught"
}
//expects either a String or your own closure, with String will do case insensitive find
static find (match) {
Closure matchClosure
if (match instanceof Closure)
matchClosure = match
if (match instanceof String) {
matchClosure = {it.toUpperCase().contains(match.toUpperCase())}
}
def inlist = UoMList.find (matchClosure)
}
static findWithIndex (match) {
Closure matchClosure
if (match instanceof Closure)
matchClosure = match
else if (match instanceof String) {
matchClosure = {it.toUpperCase().contains(match.toUpperCase())}
}
def position = UoMList.findIndexOf (matchClosure)
position != -1 ? [UoMList[position], position] : ["Not In List", -1]
}
}
i'd appreciate the secret of doing this for a static utility class rather than instance level property interception, and doing it in class declaration - not by adding to metaClass before i make the calls.
just so you can see the actual class, and script that calls - i've attached these below
my script thats calling the class looks like this
println UnitOfMeasure.list()
def (uom, position) = UnitOfMeasure.findWithIndex ("Day")
println "$uom at postition $position"
// works UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "accessed prop called $name"}
println UnitOfMeasure[4]
println UnitOfMeasure.'Per'
which errors like this
[Each, Per Month, Days, Months, Years, Hours, Minutes, Seconds]
Days at postition 2
Years
Caught: groovy.lang.MissingPropertyException: No such property: Per for class: com.softwood.portfolio.UnitOfMeasure
Possible solutions: uom
groovy.lang.MissingPropertyException: No such property: Per for class: com.softwood.portfolio.UnitOfMeasure
Possible solutions: uom
at com.softwood.scripts.UoMTest.run(UoMTest.groovy:12)
Static version of propertyMissing method is called $static_propertyMissing:
static def $static_propertyMissing(String name) {
// do something
}
This method gets invoked by MetaClassImpl at line 1002:
protected static final String STATIC_METHOD_MISSING = "$static_methodMissing";
protected static final String STATIC_PROPERTY_MISSING = "$static_propertyMissing";
// ...
protected Object invokeStaticMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) {
MetaClass mc = instance instanceof Class ? registry.getMetaClass((Class) instance) : this;
if (isGetter) {
MetaMethod propertyMissing = mc.getMetaMethod(STATIC_PROPERTY_MISSING, GETTER_MISSING_ARGS);
if (propertyMissing != null) {
return propertyMissing.invoke(instance, new Object[]{propertyName});
}
} else {
// .....
}
// ....
}
Example:
class Hello {
static def $static_propertyMissing(String name) {
println "Hello, $name!"
}
}
Hello.World
Output:
Hello, World!

Groovy call field

I'm trying to put into the field an object that supports a call operation, and then to call him. I can do it without intermediate reading fields in a variable?
My attempt looks like this:
class CallableObjectDynamic {
def call() {
return "5"
}
}
class MyClassDynamic {
CallableObjectDynamic field = new CallableObjectDynamic()
}
class GroovyRunnerDynamic {
static String make(int arg1) {
MyClassDynamic x = new MyClassDynamic()
return x.field()
}
}
​
But I receive groovy.lang.MissingMethodException.
What can you do? Or can anyone give a proof where it's written that we can't call the field?
Membership (.) has lower order of precedence than function/method/call invocation (()). Thus this line:
return x.field()
is interpreted as "invoke the 'field' method on the 'x' object".
To get Groovy to parse the code as you desire, the minimal change would be to regroup using parentheses, as follows:
return (x.field)()
which is (ultimately) interpreted as "invoke the 'call' method on the 'field' object member of the 'x' object", as desired.
It is trivial issue. Not required to have parenthesis for field.
Change from:
return x.field()
To:
return x.field
If you want to execute call method further, then use below code snippet.
Note that static method return type is changed.
class CallableObjectDynamic {
def call() {
return "5"
}
}
class MyClassDynamic {
CallableObjectDynamic field = new CallableObjectDynamic()
}
class GroovyRunnerDynamic {
static def make(int arg1) {
MyClassDynamic x = new MyClassDynamic()
return x.field
}
}
​GroovyRunnerDynamic.make(1)​.call()​
Output would be : 5
Not sure why argument to make method is done here, seems to be not used in the above code.
Alternatively, you can change
class GroovyRunnerDynamic {
static def make(int arg1) {
MyClassDynamic x = new MyClassDynamic()
return x.field.call()
}
}
​GroovyRunnerDynamic.make(1)
EDIT: Based on OP's implicit call.
Not really sure how it is working, but the below does implicit call. Just assign x.field to a variable and just add parenthesis for that as shown below.
class GroovyRunnerDynamic {
static String make(int arg1) {
MyClassDynamic x = new MyClassDynamic()
def fun = x.field
fun()
}
}
GroovyRunnerDynamic.make(1)

Spock Mock not working for unit test

I am getting weird results from a Spock unit test that I thought was being caused by a misuse of Groovy's TupleConstructor annotation. However, thanks to the help of another user, I see it is a problem with the way Spock is creating mocks. Although I have fixed the issue by replacing the injected mocks with real instances, I need to in fact get mocks working here.
My main classes:
#Canonical
#TupleConstructor(callSuper = true)
abstract class Vehicle {
Long id
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
abstract class Foobaz extends Vehicle {
String name
String label
String description
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
class Fizz extends Foobaz {
// This is an empty class that creates a meaningful name over the
// abstract Foobaz parent class. This may seem like bad design in
// this analogy, but I assure you it makes sense (from a Domain-Driven
// Design perspective) in my actual application.
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
class Car extends Vehicle {
Fizz fizz1
Fizz fizz2
#Override
String toString() {
"${fizz1.name} - ${fizz2.name}"
}
}
My Spock test:
class CarSpec extends Specification {
def "toString() generates a correct string"() {
given: "a Car with some mocked dependencies"
String f1 = 'fizzy'
String f2 = 'buzzy'
Fizz fizz1 = Mock(Fizz)
Fizz fizz2 = Mock(Fizz)
fizz1.name >> f1
fizz2.name >> f2
Car car = new Car(1L, fizz1, fizz2)
when: "we call toString()"
String str = car.toString()
then: "we get a correctly formatted string"
"${f1} - ${f2}" == str
}
}
But when I run this I get the following failure/error:
Condition not satisfied:
"${f1} - ${f2}" == str
| | | |
fizzy buzzy | null - null
false
<omitting details here for brevity>
Expected :null - null
Actual :fizzy - buzzy
Any ideas where I'm going awry?
If you change your specification to this:
class CarSpec extends Specification {
def "toString() generates a correct string"() {
given: "a Car with some mocked dependencies"
String f1 = 'fizzy'
String f2 = 'buzzy'
Fizz fizz1 = Mock()
Fizz fizz2 = Mock()
Car car = new Car(1L, fizz1, fizz2)
when: "we call toString()"
String str = car.toString()
then: "we get a correctly formatted string + getProperty('name') is called once on each Mock"
"$f1 - $f2" == str
1 * fizz1.getProperty('name') >> f1
1 * fizz2.getProperty('name') >> f2
}
}
So you define the interactions in the then block, then it should all work fine...
From our discussion on a different one of #smeeb's questions, I looked into this a bit more since I was very confused why this wasn't working.
I created my own test.
class SomeTest extends Specification {
static class Driver {
String getName(Superclass superclass) {
return superclass.name
}
}
static abstract class Superclass {
String name
}
static class Subclass extends Superclass {
}
def 'test'() {
given:
def driver = new Driver()
def subclass = Mock(Subclass)
subclass.name >> 'test'
expect:
driver.getName(subclass) == 'test'
}
}
It failed with the same problem that #smeeb saw.
driver.getName(subclass) == 'test'
| | | |
| null | false
| Mock for type 'Subclass' named 'subclass'
I tried changing a few different things and found that when I either removed the abstract modifier from Superclass or changed the return superclass.name to return superclass.getName() the test began working.
It seems there is a weird interaction on the Groovy-level between getting inherited public fields from an abstract superclass using the auto-generated accessors.
So, in your case either remove abstract modifier from FooBaz, or change your code to:
#Override
String toString() {
"${fizz1.getName()} - ${fizz2.getName()}"
}

How to check whether a Groovy class overrides a parent's method

I have two Groovy classes, Child1 and Child2, which derive from an abstract parent class Parent. The parent class implements three methods, only different in their parameters (i.e. their method names are the same).
Now I have an instance of the child class. The task is to check whether that object's class implements (overrides) one or more of the parent's methods.
I tried with Groovy's Inspector on the child class object. This gives me a list of all methods, but I am not sure how to read the output. In particular I do not understand whether the method I am looking for is implement in the child class, or only in the parent class.
Can anybody help me with this, do I maybe need another way of introspection?
Hope this helps. I still feel there is a groovier way. Try using collectInheritedMethods() to see if you get what you wanted. I deliberately wanted to return a list of matched methods instead of just a flag because you can also see the methods that are implemented from super class, Groovy truth (if(collectInheritedMethods(..))) on the list will be sufficient for flagging.
abstract class Parent {
void method1() { println "No Param" }
void method1( def a ) { println "param: $a" }
void method1( List a, Map b ) { println "param: $a and $b" }
}
class Child extends Parent {
void method1() { println "A: no param" }
void method1( def a ) { println "A: param $a" }
void method1( def a, def b ) { println "A: param $a and $b" }
}
List collectInheritedMethods( Class parent, Class child ) {
List inheritedMethods = []
def parentMethods = parent.declaredMethods.findAll { !it.synthetic }
def childMethods = child.declaredMethods.findAll { !it.synthetic }
for( chMet in childMethods ) {
for( paMet in parentMethods ) {
if( chMet.name == paMet.name &&
chMet.parameterTypes == paMet.parameterTypes &&
chMet.returnType == paMet.returnType ) {
inheritedMethods << child.getMethod( chMet.name,
chMet.parameterTypes )
.toGenericString()
//if only a flag is required to check if method implemented
//flag = true
//break
}
}
}
//Groovier way
/*inheritedMethods = childMethods.findAll { chMet ->
parentMethods.findAll { paMet ->
chMet.name == paMet.name &&
chMet.parameterTypes == paMet.parameterTypes &&
chMet.returnType == paMet.returnType
}
}*/
inheritedMethods
}
assert collectInheritedMethods( Parent, Child ) ==
["public void Child.method1()",
"public void Child.method1(java.lang.Object)"]
So I ended up with this solution, which does exactly what I wanted:
def pattern = ~/^public void ... $/
for(method in obj.metaClass.methods.findAll { it ==~ pattern })
{
/* Do some stuff... */
}

Resources