# Mock Dependencies Manually

HyperTest provides a way for you to manually mock certain dependencies that are not instrumented automatically. Allows you to capture mocks with a readable input, which will help you better understand the mocks on the dashboard.

Please check the list of [automatically instrumented packages](https://docs-v2.hypertest.co/user-guides/java-sdk/supported-java-packages), before proceeding with manual mocks.

These mocks also come in handy when you want to reduce false positives in the test reports caused by a utility that returns arbitrary values e.g., timestamps, and UUIDs.&#x20;

Let's look at an example where manual mocks can be utilized.

Below is an API route that generates PROMO code for a user.

```javascript
@RestController
@RequestMapping("/api")
public class PromoCodeController {

    @Autowired
    private UserRepository userRepository;

    // Generate promo code based on userId
    private String generatePromoCode(Long userId) {
        String uniquePart = UUID.randomUUID().toString().split("-")[0]; // Take the first part of a UUID
        return "PROMO-" + uniquePart + "-" + userId;
    }

    @PostMapping("/generate_promo_code")
    public ResponseEntity<?> generatePromoCode(@RequestBody UserRequest userRequest) {
        // Retrieve user from the database using emailId
        User user = userRepository.findByEmailId(userRequest.getEmailId());

        if (user == null) {
            return ResponseEntity.status(404).body("User not found");
        }

        // Generate promo code
        String promoCode = generatePromoCode(user.getId());

        return ResponseEntity.ok(new PromoCodeResponse(promoCode));
    }
}

```

If we record this API interaction and run a Test then HyperTest will report a value-modified error, but this is a false positive and it's not a real change that was made in the logic.

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

To eliminate this recurrent issue we can manually mock the generation of the Promo code. When we re-record this API interaction we will also have the exact value of the Promo code that was generated and the same will be used in the Test.

```javascript
@RestController
@RequestMapping("/api")
public class PromoCodeController {

    @Autowired
    private UserRepository userRepository;

    // Generate promo code based on userId
    @HtManualMock(configClass = GeneratePromoCodeConfig.class)
    private String generatePromoCode(Long userId) {
        String uniquePart = UUID.randomUUID().toString().split("-")[0]; // Take the first part of a UUID
        return "PROMO-" + uniquePart + "-" + userId;
    }

    @PostMapping("/generate_promo_code")
    public ResponseEntity<?> generatePromoCode(@RequestBody UserRequest userRequest) {
        // Retrieve user from the database using emailId
        User user = userRepository.findByEmailId(userRequest.getEmailId());

        if (user == null) {
            return ResponseEntity.status(404).body("User not found");
        }

        // Generate promo code
        String promoCode = generatePromoCode(user.getId());

        return ResponseEntity.ok(new PromoCodeResponse(promoCode));
    }
    
    public class GeneratePromoCodeConfig implements HtManualMockConfig {
        @Override
        public String generateIdentifier(Object... functionArgs) throws Exception {
            return "generatePromoCode";
        }
        
        @Override
        public EnumManager.FunctionTypeEnum getFunctionType() {
            return EnumManager.FunctionTypeEnum.SYNC;
        }
        
        @Override
        public Object[] normalizeArguments(Object... functionArgs) {
            return functionArgs;
        }
    }
}

```

To create a manual mock you need to follow these steps:

* Add annotation: Add an annotation (@HtManualMock) above the method you want to manually mock. This annotation accepts an argument called configClass.
* Create a config class: Create a config class for this mock. This class must implement ***HtManualMockConfig*** interface. You have to implement 2 mandatory methods and 1 optional method:
  * generateIdentifier(Object ...args): It accepts the same arguments as `originalFn`. It should return a string which will be your unique identifier (An identifier is needed to pick the right mock during REPLAY, if you are making the same kind of function calls then consider adding a number to the identifier).
  * getFunctionType(): It should return an ENUM (EnumManager.FunctionTypesEnum). It tells us that that method you are trying to manually mock is a SYNC/ASYNC/CALLBACK method.
  * normalizeArguments(Object... args): This method is optional and normalizes or transforms the input arguments. It accepts the same arguments as `originalFn` and returns an object with normalized input values. Return value of this function will be used as readableInput and would be used for display on dashboard.

If we run a test with the manual mock in place it passes.

<figure><img src="/files/3LrWXF7AUkSRlYIL7Z98" 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/user-guides/java-sdk/mock-dependencies-manually.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.
