0
votes

I need to know how to log an error when a JDBC request returns no data from DB in Jmeter.

I have got:

JDBC request

Query:

SELECT names 
FROM   customers 

Variable names: namesCustomers

Then I have got a BeanShell postProcessor with script:

import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("namesCustomer", vars.get("namesCustomer_1"));

And finally a simple controller called "set variables" which calls an external file with the following code: vars.put("namesCustomer",JMeterUtils.getProperty("namesCustomer"));

The problem is that in another SOAP request I am using the variable namesCustomer_1, and if no data is returned from DB this request fails. I need to log an error when the JDBC returns no data.

If I add to the post processor:

log.error("Error example");

I see the error logged in jmeter.log when this request is ran. I need something like:

if(JMeterUtils.setProperty("namesCustomer", vars.get("namesCustomer_1")).toString().length()==0){
log.error("DB returned no results")
}

Any Ideas on how to log an error when the JDBC request returns no data?

1

1 Answers

0
votes

It can be done a little bit easier. If JMeter Variable is not set, it'll be null, not empty string.

So I would suggest amending your code as follows:

String namesCustomer = vars.get("namesCustomer_1");

if (namesCustomer == null) {
    log.error("namesCustomer_1 variable is not set");
} else {
    props.put("namesCustomer", namesCustomer);
}

props is a shorthand to access current set of JMeter Properties, there is no need to use JMeterUtils.setProperty() method.

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.