public static String concatenateWithThreshold(String a, String b, int threshold) {
    // Convert both strings to byte arrays in UTF-8 encoding
    byte[] bytesA = getUTF8Bytes(a);
    byte[] bytesB = getUTF8Bytes(b);
    if (bytesB.length > threshold) {
      throw new IllegalArgumentException(String.format(
          "Length of the Second string to concatenate exceeds the threshold (%d > %d)",
          bytesB.length, threshold));
    }

    // Calculate total bytes
    int totalBytes = bytesA.length + bytesB.length;

    // If total bytes is within the threshold, return concatenated string
    if (totalBytes <= threshold) {
      return a + b;
    }

    // Calculate the maximum bytes 'a' can take
    int bestLength = getBestLength(a, threshold - bytesB.length);

    // Concatenate the valid substring of 'a' with 'b'
    return a.substring(0, bestLength) + b;
  }
@Test
  void testConcatenateInvalidInput() {
    // Test case when b alone exceeds the threshold
    String a = generateRandomString(900);
    String b = generateRandomString(3000); // 3000 bytes in UTF-8
    Exception exception = assertThrows(IllegalArgumentException.class, () -> {
      concatenateWithThreshold(a, b, 2048);
    });

    String expectedMessage = "Length of the Second string to concatenate exceeds the threshold (3000 > 2048)";
    String actualMessage = exception.getMessage();

    assertTrue(actualMessage.contains(expectedMessage));
  }