-1

Here i'm facing one problem that i have to do unit testing of 16 Digits of Generated Random( 9872 5432 8765 0987, 9852 7412 6587 2365.........n) number so these Random Number are generated from Sql queries, i have to check uniqueness How to do unit testing for this scenario is there any approach, or code or tool to perform this operation please tell me with simple demo

Thanks in advance

7
  • 1
    This is not a site for asking people to send you demos that solve your problems.
    – user246
    Mar 16, 2018 at 15:04
  • What should you test? Test that the numbers are random or that they are unique and have a certain length?
    – Alexey R.
    Mar 16, 2018 at 15:05
  • You mentioned two requirements: number length and uniqueness. Which one are you having trouble with?
    – user246
    Mar 16, 2018 at 15:07
  • 1
    I'm confused - what is your actual problem? What have you tried so far?
    – corsiKa
    Mar 16, 2018 at 16:55
  • 1
    Validating the uniqueness implies testing all the combinations or at least enough to assert a certain level of confidence, which should take you between 50hrs to 100hrs with a desktop computer. And yet it wouldn't be enough to cover some cases if the generator where to rely on a timestamp. Instead of testing the uniqueness, you should validate the algorithm.
    – Florent B.
    Mar 16, 2018 at 17:40

1 Answer 1

1

You do not need any special tool. Just pure Java and some maths (or minimal search-over-the-internet-or-at-least-over-the-stackexchange skills).

Here is your demo.

You only need to rework it for your input and for Assertion mechanism of your unit-test framework.

public class TestNumbers {

    final static int numberLength = 3;

    public static void main(String[] args) {

        int[] testArrayOk = new int[]{
            123,
            321,
            111,
        };
        int[] testArrayWithNoUnique = new int[]{
            123,
            321,
            123,
        };
        int[] testArrayWithWrongLength = new int[]{
                123,
                321,
                12,
        };

        testAll(testArrayOk);
        testAll(testArrayWithNoUnique);
        testAll(testArrayWithWrongLength);
        
    }

    static void testAll(int[] inputArray){
        testForDups(inputArray);
        testForLength(inputArray);
    }
    
    static boolean testForDups(int[] inputArray){
        Set<Integer> findDupsSet = new HashSet<Integer>();
        for (int item : inputArray)
        {
            if (findDupsSet.contains(item)) {
                System.out.println("Dup detected in array: " + Arrays.toString(inputArray));
                return true;
            }
            findDupsSet.add(item);
        }
        return false;
    }

    static boolean testForLength(int[] inputArray){
        for(int i: inputArray){
            if(1 + (int)Math.log10(i) != numberLength){
                System.out.println("Item of improper length detected in array: " + Arrays.toString(inputArray));
                return true;
            }
        }
        return false;
    }
}

Output:

Dup detected in array: [123, 321, 123]

Item of improper length detected in array: [123, 321, 12]

Solutions are taken from:

Not the answer you're looking for? Browse other questions tagged or ask your own question.