# Java

## Prerequisites

### 1. Install/update ht-cli npm package

```
npm install -g @hypertestco/ht-cli --save-exact
```

Verify htcli has been installed using the below command:

```
htcli --help
```

## Getting Started:

### 1. Create a Test Configuration File

Navigate to root directory of your application and create a `.htTestConf.js` file

Available Configurations:

<pre class="language-javascript" data-title=".htTestConf.js" data-full-width="false"><code class="lang-javascript"><strong>const requestTypes = {
</strong>  HTTP: 'HTTP',
  GRAPHQL: 'GRAPHQL',
  KAFKA: 'KAFKA',
  GRPC: 'GRPC',
  AMQP: 'AMQP',
};

module.exports = {
  htBackendBaseUrl: "", // URL of HyperTest server (Required)
  htCliRefreshToken: "",  // Auth token for the CLI (Required)
  serviceIdentifier: "", // UUID for the service (Required)
  requestTypesToTest: [requestTypes.HTTP], // What kind of requests to include in the test
  httpCandidateUrl: "", // HTTP URL of App under test (Optional)
  // graphqlCandidateUrl: "", // GraphQL URL of App under test (Optional)
  appStartCommand: "mvn", // Command to start the app (Required)
  appStartCommandArgs: ["spring-boot:run"],  // App start command arguments (Required)
  appWorkingDirectory: __dirname, // Working directory for the app (default: current working dir) (Optional)
  appStartTimeoutSec: 30, // Timeout in seconds for the start command (default: 10) (Optional)
  showAppLogs: true, // Whether to show app logs (default: false) (Optional)
  shouldReportHeaderDiffs: false, // Whether to report differences in headers (default: false) (Optional)
  testBatchSize: 50, // Number of concurrent test requests (default: 50) (Optional)
  //testRequestsLimit": 10, // Number requests to test (Optional)
  //httpReqFiltersArr: [], // "&#x3C;GET /users>", "&#x3C;ANY REGEX:^/payments>" (Optional)
  htExtraHeaders: { // Object containing additional headers for HyperTest server requests (Optional)
    // authorization: 'Bearer xyz'
  },
  fastMode: true, // Default false. (aggregate requests only on the basis of request input and output schema - ignoring mock schemas)
  // httpReqsToTest: [], // specific http requests to be tested can be mentioned. Request Id can be taken from "All Requests" page in dashboard.
  // graphqlReqsToTest: [], // specific graphql requests to be tested can be mentioned.
  // grpcReqsToTest: [], // specific grpc requests to be tested can be mentioned.
  // kafkaReqsToTest: [], // specific kafka requests to be tested can be mentioned.
  // amqpReqsToTest: [], // specific amqp requests to be tested can be mentioned.
  // tags: [{name: '', value: ''}], // requests which contain the mentioned tags will be tested. Refer Tags under "User Guides/Node.js SDK" for more information.
  // shouldIgnoreErrStackTraceDiffs: true, // Stack trace differences are ignored in errors.(default: true) (Optional)
  
  // filterFunctionToIgnoreMockDiffs:({ mockDiff, currentMock, requestObj }) => { 
  //  // if false is returned then the diff will be ignored
  //  if(mockDiff?.originalValue?.langType === 'Date') return false;
  //  if(mockDiff?.evaluatedPath?.at(-1) === "url" || mockDiff?.evaluatedPath?.at(-2) === "headers") return false;
  //  if(mockDiff?.evaluatedPath?.at(-1) === "host") return false;
  //  return true;
  //},
  
  // filterFunctionToIgnoreResponseDiffs: ({ responseDiff, requestObj }) => { // Param Types are mentioned in Type References page
  // // if false is returned then the response difference will be ignored  
  //  if(responseDiff?.evaluatedPath?.at(-1) === "url" || evaluatedPath?.evaluatedPath?.at(-2) === "headers") return false;
  //  if(responseDiff?.evaluatedPath?.at(-1) === "host") return false;
  //  return true;
  //},
  // exitCodeSetter({ testResult }) {
  //  console.log('==test results==')
  //  console.log(testResult)
  //  return 0;
  //},
  // initialTimestamp: "", // Initial timestamp in ISO format (Optional)
  // finalTimestamp: "", // Final timestamp in ISO format (Optional)
  // cliServerHost: "", // HT CLI server Host to be Used by Clients(server ignores this) (default: localhost) (Optional)
  // sdkServerHost: "", // HT SDK server Host to be Used by Clients(server ignores this) (default: localhost) (Optional)
  // autoAcceptChangesInCaseOnlyNoiseDetected: false,
  // shouldIgnoreErrStackTraceDiffs: true,
  // exclusionStringsForDifferences: [], // e.g., ['01\.02\.03\.04', 'HyPeRtEsT'],
  // reservedAppPorts: [], // Ports used by the host application e.g., [3001,4001]
};
</code></pre>

Please refer this [link](/user-guides/node.js-sdk/type-references.md) for param type references in filterFunctionToIgnoreMockDiffs and filterFunctionToIgnoreResponseDiffs.

### Troubleshooting

#### Alternative Approach #1: Using `java -cp`

1. **Navigate** to your application's root directory.
2. **Compile** the application:

   ```bash
   mvn clean install
   ```
3. **Copy Dependencies**:

   ```bash
   mvn dependency:copy-dependencies -DoutputDirectory=target/dependency
   ```
4. **Update `.htConfig.js`:**
   * Replace `<YOUR-APPLICATION-MAIN-FILE>` with your main class.

**Updated `appStartCommand` Configuration:**

```javascript
appStartCommand: "java", // Command to start the application (Required)
appStartCommandArgs: [
  "-cp",
  "target/classes:target/dependency/*",
  "<YOUR-APPLICATION-MAIN-FILE>"
]
```

#### Alternative Approach #2: Using `bazel run`

1. **Navigate** to your application's root directory.
2. **Run the Application**:

   ```bash
   bazel run //:<YOUR-BAZEL-MODULE>
   ```
3. **Update `.htConfig.js`:**
   * Replace `<YOUR-APPLICATION-MAIN-FILE>` with your main class.

**Updated `appStartCommand` Configuration:**

```javascript
appStartCommand: "bazel", // Command to start the application (Required)
appStartCommandArgs: [
  "run",
  "",
  "<YOUR-APPLICATION-MAIN-FILE>"
]
```

### 2. CLI token generation

Please follow this [link](/user-guides/node.js-sdk/cli-login.md#id-1-if-running-test-in-ci) to complete token generation if test is being run in CI env.

Please follow this [link](/user-guides/node.js-sdk/cli-login.md#id-2-if-running-test-on-local) to complete CLI login if test is being run in local.

### 3. Start new test

Start a new test by running this command.

```
htcli start-new-test --config-file-path ./.htTestConf.js
```

Open the help to list all possible options available

```
htcli start-new-test --help
```

### 4. Test reports

After the test is completed. You can see the results on the dashboard under Test Results

<figure><img src="/files/hRp5HWFvz1nWqcwFh7or" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-v2.hypertest.co/setup-guide/start-a-test-run/start-a-test-run-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
