Before hook in java

package simulations;

import api.CustomHeaders;
import api.StandardHeader;
import api.URLs;
import helper.IdentityHelper;
import helpers.ConfigProvider;
import helpers.Device;

import io.gatling.javaapi.core.FeederBuilder;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.http.HttpProtocolBuilder;
import models.Config;
import static io.gatling.javaapi.core.CoreDsl.csv;
import static io.gatling.javaapi.core.CoreDsl.rampUsers;
import io.gatling.javaapi.core.Simulation;
import static io.gatling.javaapi.http.HttpDsl.http;


public class FastLoginSimulation extends Simulation {

    FeederBuilder<String> feeder = csv("customer2.csv").queue();
    private Device device = new Device();
    private IdentityHelper identityHelper = new IdentityHelper();
    private String deviceId = "#{deviceId}";
    //private Config config = ConfigProvider.getConfig();
    private Config config;

    @Override
    public void before() {
        // logger.info("Getting environment config");
        System.out.println("Simulation is about to start!");
        config = ConfigProvider.getConfig();
    }

    HttpProtocolBuilder httpProtocol = http
            .baseUrl(config.getApiBaseUri())
            .header(CustomHeaders.DEVICE_ID, deviceId)
            .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
            .userAgentHeader("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 " +
                    "(KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36")
            .header(StandardHeader.CONTENT_TYPE, "application/json");

    ScenarioBuilder scn = identityHelper.login("login", feeder, device);

    {
        setUp(scn
                .injectOpen(rampUsers(1).during(1))
                .protocols(httpProtocol)
        );
    }
}

Does the before hook initialize after HttpProtocolBuilder because when I run the above script it gives a null pointer because config object is null but if I remove before hook and initialize config directly at class level it works.

That’s the whole purpose of this before hook
The simulation is initialized but not yet started.

Cheers!

1 Like

But will before hook initialise before the simulation. Looks like before hook is not running before httpprotocol builder.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.