I'm trying to write a parameterized test using a csv file as the data source. I would like to check the json node for a number if keys, if the are present. Here my first attempt:
#ParameterizedTest(name = "{index} => name={0}")
#CsvFileSource(resources = "fields.csv", numLinesToSkip = 0)
void verifyFields(String name) {
Response response = getResponse(); // rest - assured query response
response
.then()
.assertThat()
.body("components.any { it.containsKey('" + name + "') }", is(true));
But I keep getting the following error when trying to verify that the key xzy is present:
Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.HashMap$Node.containsKey() is applicable for argument types: (String) values: [xyz]
I'm not sure I'm using the groovy statement correctly. I'm just trying to use the parameters from the file.
Related
I am trying to parse through some properties file using configslurper.
ENT.adminserver.nodenumber=1
ENT.managedserver.1.host=vserver04
ENT.managedserver.2.host=vserver05
ENT.managedserver.3.host=vserver08
ENT.managedserver.4.host=vserver07
Said properties file. I am trying to read the host names from the properties.
Properties properties = new Properties()
File propertiesFile = new File('DomainBuild.properties')
propertiesFile.withInputStream {properties.load(it)}
def config = new ConfigSlurper().parse(properties)
def domainname="ENT" //will be passed through paremeters
def domain = config.get(domainname)
def managedServerFlow= {
println domain.managedserver
println domain.managedserver.keySet()
domain.managedserver.each {
println it.getClass()
println it.get("1")
}
for (server in domain.managedserver) {
println server.getClass()
println server
}
}
}
the it.get("1") is causing the following error.
No signature of method: java.util.LinkedHashMap$Entry.get() is applicable for argument types: (java.lang.String) values: [1]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), getKey(), any()
I looked through the java and groovy doc and spent few hours without resolution. Please help.
Instead of
println it.get("1")
Try
println it.'1'
Or
println it.getAt("1") // as the exception shows you
Think about what types you are working with. config is a ConfigObject, which you can treat like a map. Its sub-objects domain and domain.managedserver are also ConfigObjects. When you call each on domain.managedserver and pass it a closure that takes no parameters, it gives you a set of Entries. Therefore you can't call it.get("1") because an Entry doesn't have a property called "1". It has key and value. So you can either println "$it.key: $it.value" or
domain.managedserver.each { key, value ->
println value.getClass()
println "$key: $value"
}
or if you want to get the value for key "1" directly:
println domain.managedserver.'1'
I'm writing an API that sends stuff through a socket. What I want to do, is have a basic response like so:
{
method: ID,
id: 10
}
The first tag being the type of stuff being sent, and everything after that is content. I can't seem to figure out how to do that with Groovy's JsonBuilder.
I'm reading the documentation for that, and I attempted to do:
String message = builder.value {
method: Method.ID
id: id
}
But it only generates: {value={}}. Also, it contains the value tag (which I don't want).
I tried doing builder {...}, but that caused this error:
Exception in thread "Thread-10" groovy.lang.MissingMethodException: No signature of method: website.DerbyProManagerService.builder() is applicable for argument types: (website.DerbyProManagerService$DerbyProInstance$_newId_closure1) values: [website.DerbyProManagerService$DerbyProInstance$_newId_closure1#6db853f4]
Try this instead:
String message = builder.call {
method Method.ID
id myId
}
I have two groovy scripts A and B.
A looks like:
import some.Class
//Some helper function
String doSomeWork(Integer input) {
//does something with input and returns a String
return "result: $input"
}
//Now do some real stuff here
println( doSomeWork(42) )
In B I want to import A (its in the classpath) and use its doSomeWork:
import A
println A //prints class A. This means we can access it here. =)
println( A.doSomeWork(45) ) //Now try to call As doSomeWork
The last line however results in an exception:
Caught: groovy.lang.MissingMethodException: No signature of method: static A.doSomeWork() is applicable for argument types: (java.lang.Integer) values: [45]
Possible solutions: doSomeWork(java.lang.Integer)
groovy.lang.MissingMethodException: No signature of method: static A.doSomeWork() is applicable for argument types: (java.lang.Integer) values: [45]
Possible solutions: doSomeWork(java.lang.Integer)
at B.run(B.groovy:3)
import A //Will execute As code
println A //prints class A. This means we can access it here. =)
println( (new A()).doSomeWork(45) ) //Now call As doSomeWork
What do I have to do to successfull call As doSomeWork in script B?
EDIT: Okay, I found out. it is not a static method but a method of A. So (new A()).doSomeWork(45) is doing the job. However, that means when A is imported, all its code is executed before Bs code. How can I avoid that? My goal is to only use As method (which obvioulsy does not access any of As properties) without A doing any other side effects.
I'm trying to let a script assertion fail, when a variable has another value than defined. My goal: Mark the TestStep as red, if the TestStep will fail. Please note that I am using a script assertion for a TestStep - not a separate Groovy-Script TestStep.
My script looks like this:
httpResponseHeader = messageExchange.responseHeaders
contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
if (contentType != "[image/jpeg]"){
log.info("ERROR! Response is not an image.")
//Script Assertion should fail.
//TestStep should be marked red.
} else {
log.info("OK! ResponseType is an image.")
}
Is there any way, how to let the script assertion fail depending on a property?
I've tried to use the getStatus() method, but this is only available for the testrunner object. Unfortunately the testRunner-object can not be used within a script assertion concerning this post: http://forum.soapui.org/viewtopic.php?f=2&t=2494#p9107
Need to assert so that test fails or passes automatically based on the condition.
def httpResponseHeader = messageExchange.responseHeaders
def contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
assert contentType.get(0) == "image/jpeg","Content Type is not an image"
EDIT: Earlier paid attention to how to throw error, assuming you have the rest in place. Now changed last line of code based on the input.
I want to write a unit test (by JUnit) to test value of this function in Groovy:
String getPeopleNamesById(int[] peopleIds) {
List<String> names = People.createCriteria().list{
projections { property("name") }
'in' ("id", peopleIds)
}
return names ? names.join(", ") : "";
}
But the unit test always fails when reading this statement: List names = People.createCriteria().list{...}
groovy.lang.MissingMethodException: No signature of method: People.createCriteria() is applicable for argument types: () values: [].
I guess it because of calling to functions which execute some DB connections and queries?
Could you please help me write this test? Thank you so much!
Criteria queries aren't available in unit tests, and aren't provided by mockDomain. You can either mock your criteria queries yourself, e.g. with mockFor, or make your test an integration test, where you have access to a full database environment.
Here's an example of how you could mock your query:
mockFor(People).demand.static.createCriteria = { ->
[list: { closure -> [ <some mock objects> ] } ]
}