JUnit test case for array used for concatenations

by Vartika   Last Updated July 12, 2019 08:26 AM

this the concatenation of array code I tried writing the junit test case for it but its the giving the Syntax error. I am not sure whats the exact syntax.

public static String[] concatArray(String[] firstArry, String[] secArry) {
        if (null != firstArry && null == secArry) {
            return firstArry;
        } else if (null == firstArry && null != secArry) {
            return secArry;
        } else if( null != firstArry && null != secArry ){
            String[] concatArry = new String[firstArry.length + secArry.length];
            System.arraycopy(firstArry, 0, concatArry, 0, firstArry.length);
            System.arraycopy(secArry, 0, concatArry, firstArry.length, secArry.length);
            return concatArry;
            return null;

JUnit test case for it.

    public void testConcatArray() {
        String[] arr1 = {"nodename", "nodecategory"};
        String[] arr2 = {" test"," case"};

        Assert.assertEquals({"abc"}, StringUtils.concatArray(arr1, arr2));


Tags : java junit

Answers 2

The compiler does not know what is the actual type of the first param.

You need to use the following allong with a different assertion:

Assert.assertArrayEquals(new String[]{"abc"}, StringUtils.concatArray(arr1, arr2));
Maciej Kowalski
Maciej Kowalski
July 12, 2019 08:21 AM

you can assert arrays using the assertArrayEquals() method.

see: Using assertArrayEquals in unit tests

maybe you want to take a look at Java Stream API afaik it can also handle the concatenation of null arrays so you don't have to care about the null handling. e.g. if you have an array ['abc'] and null the Stream concat would return ['abc']. ( but maybe you need to filter before I am not 100% sure so: yourStream.filter(array -> array != null))

July 12, 2019 08:25 AM

Related Questions

Updated January 23, 2019 05:26 AM

Updated April 03, 2017 08:26 AM

Updated September 30, 2017 14:26 PM

Updated February 21, 2019 22:26 PM

Updated January 23, 2019 23:26 PM