How to get around spock Data provider is null error - groovy

I have a SpringBootTest that reads in properties from application.properties. The setup code uses the #Value annotation to set the values accordingly. One of these properties is an array of names.
I am trying to write a data driven test using Spock. The where statement is using these names that are initialized in the setup:
expect:
retrievedName == value
where:
value << getNames()
This always fails with org.spockframework.runtime.SpockExecutionException: Data provider is null.
It appears that the getNames() call is invoked before the properties are initialized in the setup code. If I do not use the where statement (data driven testing), all works fine. Is there a workaround for this?

You cannot use data initialized in the setup section as a source for data driven tests. As per the docs:
Although it is declared last, the where block is evaluated before the feature method containing it runs.
You can try and use setupSpec() methods and #Shared fields as a workaround.
See here for an example.

Related

Custom library not recognized inside DSL file in groovyscript part

Context: I'm implementing a Jenkins Pipeline. For this, in order to define the pipeline's parameters I implemented the DSL file.
In the DSL file, I have a parameter of ActiveChoiceParam type, called ORDERS. This parameter will allow me to choose one or more orders numbers at the same time.
Problem: What I want to do is to set the values that gets rendered for ORDERS parameter from a custom library. Basically I have a directory my_libraries with a file, orders.groovy. In this file, there is an order class, with a references list property that contains my values.
The code in the DSL file is as follows:
def order = new my_libraries.order()
pipelineJob("111_name_of_pipeline") {
description("pipeline_description")
keepDependencies(false)
parameters {
stringParam("BRANCH", "master", "The branch")
activeChoiceParam("ORDERS") {
description("orders references")
choiceType('CHECKBOX')
groovyScript{
script("return order.references")
fallbackScript("return ['error']")
}
}
}
}
Also, is good to mention that my custom library works well. For example, if I choose to use a ChoiceParam as below, it works, but of course, is not the behaviour I want. I want to select multiple choices.
choiceParam("ORDERS", order.references, "Orders references list but single-choice")
How can I make order.references available in the script part of groovyScript?
I've tried using global instance of order class, instantiate the class directly in the groovyScript, but no positive result.

How to set a test case variable in property transfer for xpath?

I am having an issue with property transfer and test case variable in soap UI.
Basically in my property transfers I want to pull out a value from a soap request response step, but I want the some values to be picked out from a test case property like so:
declare namespace T='http://test.com'
//T:Testxml/T:NextLevel/T:FinalLevel[#date='${#TestCase#testDate}']/#price
However it doesn't like this as it spits out [null] but when I replace the variable to a hard coded date (same value as in the test case property), then it works.
My question is how can I pass through a test case variable in this scenario?

SoapUi: Transferring a response value to a temporary property (missing a non persisting target scope)

Using SoapUI Pro 5.0
I know how to transfer response values to any scoped property (test suite, test case, etc.).
My problem is, that such scoped properties:
must already exist in the target scope
are persisted at the end in the project configuration file. So after each run this configuration file is changed, what is a nightmare for source control.
I need this value only one or two step(s) later for doing complex verifications (usingg a Script-TestStep).
The "Property Transfer"-TestStep is very powerful for extracting from known sources and transferring to known targets. As target one even can choose a Script-TestStep from the same TestCase. But I did not find any hint how to bind the value to be transferred to - let's say - a declared variable within the target script.
Using the context (e.g. context.getProperties().put( 'MY_PROP_NAME', transferValue ) would be nice but the context is not available within the "Property Transfer"-TestStep. The only possibility I figured out is following script code in a Script-TestStep:
def xmlResponse = XMLNamespaceRemover.removeNamespaces(context.expand( '${mySoapTestStep#Response}' ));
def node = new groovy.util.XmlParser(false,false).parseText(xmlResponse);
def transferValue = node["Body"]["tag1"]["tag2"].text();
context.getProperties().put( 'MY_PROP_NAME', transferValue )
Any ideas how to solve this using the "Property Transfer" TestStep?
In your script, you can just use:
def MY_PROP_NAME = context.expand( '${mySoapTestStep#Response//*:Body/*:tag1/*:tag2}' )
Alternatively, you can use a Property Transfer step to transfer your value to a TestCase Property - which, as you pointed out, must already exist. In order to get around the problem you mentioned of persisting and thereby messing around with source control, you can create a TearDown script:
testRunner.testCase.setPropertyValue("MY_PROP_NAME", "default")

JsConfig<MyClass>.ExcludePropertyNames example, not working for me

Trying to exclude properties from a model from being included during serialization.
I am using the following syntax:
JsConfig<MyTestClass>.ExcludePropertyNames = new[] { "ShortDescription" };
Just after that I have the following:
return (from o in __someProvider.GetAll() select (new
{
o.Name,
o.ShortDescription
o.InsertDate
}).TranslateTo<MyTestClass>()).ToList()
However once result is returned from the method, it still contains "ShortDescription" field in the Json. Am I doing something wrong?
JsConfig<T>.ExcludePropertyNames appears to be checked only once for each type, in a static constructor for TypeConfig<T>. Thus, if you are configuring ExcludePropertyNames in your service class, just before returning your response, it might be too late -- the TypeConfig properties may already be set up and cached for MyTestClass. I was able to reproduce this.
A more reliable alternative is to move all of your JsConfig<T> configuration to your AppHost setup code.
If you really do need to do this in your service class, e.g. if you are only conditionally excluding property names, then an alternative approach would be to ensure that JsConfig.IncludeNullValues is false (I believe it is by default) and in your service code set ShortDescription to null when appropriate.

XText: permit invalid cross reference

I need to build a grammer containing a cross reference, which may be invalid, i.e. points to a nonexisting target. A file containing such a reference should not yield an error, but only a warning. The generator would handle this as as a special case.
How can I do this with XText?
It's not possible to create valid cross references to non-existing targets in EMF.
I would suggest to go with EAttributes instead of EReferences:
Change the feature=[EClass|ID] by feature=ID in {YourDSL} grammar.
Provide a scope calculation utility like it's done in *scope_EClass_feature(context, reference)* method in the {YourDSL}ScopeProvider class. As this scoping methods simply use the eType of the given reference the reimplementation should be straightforward.
Use this scope calculation utility in {YourDSL}ProposalProvider to propose values for the introduced EAttribute.
Optionally you can use this utility in a validation rule to add a warning/info to this EAttribute if it's not "valid".
Finally use the utility in your generator to create output based on valid target eObjects.
I also ran into this problem when creating a DSL to provide declerations of variables for a none-declerative language for a transition pahse. This method works but ask yourself if you realy want to have those nasty may-references.
You can drop the auto generated error in you UI module only. To do so, provide an ILinkingDiagnosticMessageProvider and override the function getUnresolvedProxyMessage:
class DSLLinkingDiagnosticMessageProvider extends LinkingDiagnosticMessageProvider {
override getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
if(context.context instanceof YourReference) {
// return null so the your error is left out
null
} else {
// use super implementation for others
super.getUnresolvedProxyMessage(context)
}
}
}
All linker-errors for YourReference will be missed. But be aware that there will be a dummy referenced object with all fealds null. Exspecialy the name ist lost and you can not set it due to a CyclicLinkingException. But you may create a new method that sets the name directly.
Note that the dummy object will have the type you entered in your gramma. If its abstract you can easily check witch reference is not linked.

Resources