2

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?

1 Answer 1

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.