Method pointers in test case variables

Intro

I have two test cases that are basically doing the same thing, just with slightly different input. After looking at the code for them both, I found that they are both expressible as functions of one variable: a callback (aka a Closure in the Groovy world).

It therefore makes sense to create a parameterized test case that accepts that Closure<Double> as a parameter, and then have the original two call that parameterized test case with the appropriate Closure.

What these Closures look like

For both test cases, I use method pointers:

  • SMDNumberUtils.&NextDollarAmount
  • SMDNumberUtils.&NextWholeDollarAmount

The methods that are being pointed to, are both static methods that return a plain-ol Double

OK, so what’s the problem?

When I try to create this parameterized test case, and create the test case variable, the Variables tab doesn’t seem to know what to do with it…

The test case variable

Script mode

When I go to Variables (Script mode) this is how I have it:

<?xml version="1.0" encoding="UTF-8"?>
<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="variableEntityWrapper">
   <description></description>
   <tag></tag>
   <variables>
      <defaultValue>com.signaturemd.utils.SMDNumberUtils.&amp;NextWholeDollarAmount</defaultValue>
      <description></description>
      <id>a80da491-cd87-4c67-b9f9-04436dc441cf</id>
      <masked>false</masked>
      <name>onGenerateAmount</name>
   </variables>
</Entity>

Manual mode

For some reason, this is how it’s coming up in Variables tab (manual mode):

Notice the lack of type. It’s not even showing up as a property!

I can’t even double click to edit!

Does your test case work besides this?

Yes, it not only works, but it reads in the default type correctly (I run it, and it is producing the amounts as random whole dollar amounts, as expected)

Not sure if I can, or should, do anything about this…

You wrote: “this is how I have it”.

But I think that you can not create this XML document by the Variables tab (Manual mode). So I wonder how you created it.

Did you type the value of the defaultValue element manually in the Script mode editor?

I created it in the Variables (Script mode)

Line#420


                String defaultValue = StringUtils.defaultIfEmpty(testCaseVariable.getDefaultValue(),
                        StringConstants.NULL_AS_STRING);
                Object defaultValueObject = engine.runScriptWithoutLogging(defaultValue, null);

The engine variable is an instance of javax.script.ScriptEngine. The runScriptWitoutLogging() method evaluates a string as a script written in Groovy language.

When @mwarren04011990 switched the script Variables tab from Script mode to Manual mode, the Line#421 tried to evaluate a string as defaultValue

com.signaturemd.utils.SMDNumberUtils.&NextWholeDollarAmount

as a self-contained Groovy script. How the evaluation result will be?

I suppose, the ScriptEngine failed. The engine possibly will be unable to evaluate the string into an Groovy Object because the class is not loaded in the runtime CLASSPATH.

I suppose, this is the reason why @mwarrent04011990 encounter the following problem:

I think that your attempt ended in vain.

If you create 2 Test Case scripts: one is Caller, another is Callee.
The Caller can be like:

WebUI.callTestCase(findTestCase('Test Cases/Callee`, 
    [ "closure": com.signaturemd.utils.SMDNumberUtils.&NextWholeDollarAmount])

This might work; the runtime CLASSPATH would have a valid reference to the com.signaturemd.utils.SMDNumberUtils class you created.

1 Like

This is precisely what I ended up doing