Expand my Community achievements bar.

Introducing Adobe LLM Optimizer: Own your brand’s presence in AI-Powered search and discovery

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

Junit testcase for uniqueId method generator

Avatar

Level 4

Hi All,

Could you please let me know how to write junit for the below method generating randomId in sling model.

public String getRandomId() {

    int n=8;

    byte[] array = new byte[256];

    new Random().nextBytes(array);

 String randomString = new String(array, Charset.forName("UTF-8"));

    StringBuffer r = new StringBuffer();

    for (int k = 0; k < randomString.length(); k++) {

        char ch = randomString.charAt(k);

        if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))&& (n > 0)) {

            r.append(ch);

            n--;

        }

    }

    return "_"+r.toString();

}

 

Thanks in Advance!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You can mock the output of generated item and have it the same value(have few more test cases with different use cases)

e.g.

https://stackoverflow.com/questions/14811014/writing-a-junit-test-for-a-random-number-generator 

Arun Patidar

AEM LinksLinkedIn

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi,

You can mock the output of generated item and have it the same value(have few more test cases with different use cases)

e.g.

https://stackoverflow.com/questions/14811014/writing-a-junit-test-for-a-random-number-generator 

Arun Patidar

AEM LinksLinkedIn