Tarek K. Ajaj
Tarek K. Ajaj

Reputation: 2507

Using Java Request Sampler inside a ForEach controller in Jmeter

I am Trying to use a Java request Sampler inside a ForEach Controller.

This is my custom Sampler

public class ClientSampler extends AbstractJavaSamplerClient {

    String Name;

    @Override
    public Arguments getDefaultParameters() {
        Arguments defaultParameters = new Arguments();
        defaultParameters.addArgument("name", "Tarek");
        return defaultParameters;
    }

    @Override
    public void setupTest(JavaSamplerContext context) {
        Name = context.getParameter("name");
    }

    @Override
    public SampleResult runTest(JavaSamplerContext context) {   
        System.out.println(Name);
    }
}

in Jmeter I create user defined variables with 5 variables:

enter image description here

And a ForEach Controller:

enter image description here

then added the java request as a child to ForEach controller:

enter image description here

the Test plan is the following:

enter image description here

when I start the test the output is:

first
first
first
first
first

expected:

first
second
third
fourth
fifth

even if I set the start and end indexes in the ForEach controller the result is the same.

using an http sampler inside the ForEach controller works great, but when using a Java requests the result is not as expected.

Can anyone explain why I am getting this output?

Upvotes: 2

Views: 5012

Answers (1)

Tarek K. Ajaj
Tarek K. Ajaj

Reputation: 2507

I solved it.

The problem is because I misunderstood how it works:

Jmeter calls SetupTest(JavaSamplerContext context) once before the Test starts and calls runTest(JavaSamplerContext context) in each loop (I though it calls SetupTest also at the beginning of each loop).

so I just added

Name = context.getParameter("name");

inside runTest and now the result is exactly how it should be.

@Override
public SampleResult runTest(JavaSamplerContext context) {   
    Name = context.getParameter("name");        
    System.out.println(Name);
}

Upvotes: 4

Related Questions