Skip to content

Instantly share code, notes, and snippets.

@kotinagam
Created May 22, 2017 05:29
Show Gist options
  • Save kotinagam/90a5b7a5a8ab4d036a13d4de8849f1ad to your computer and use it in GitHub Desktop.
Save kotinagam/90a5b7a5a8ab4d036a13d4de8849f1ad to your computer and use it in GitHub Desktop.
Junit to test FlattenArray java program
package com.myorg.agodaexercise;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests FlattenIntegerArray
*/
public class FlattenIntegerArrayTest {
Integer[] expectedArray = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@Test
public void testNullReturnsNull() throws IllegalArgumentException {
Assert.assertNull(
"Testing a null argument",
FlattenIntegerArray.flatten(null)
);
}
@Test
public void testEmptyArray() throws IllegalArgumentException {
Assert.assertArrayEquals(
"Testing an empty array",
new Integer[]{},
FlattenIntegerArray.flatten(new Object[]{})
);
}
@Test
public void testFlatArray() throws IllegalArgumentException {
Assert.assertArrayEquals(
"Testing a flat array",
expectedArray,
FlattenIntegerArray.flatten(new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
);
}
@Test
public void testNestedArray() throws IllegalArgumentException {
Assert.assertArrayEquals(
"Testing nested array",
expectedArray,
FlattenIntegerArray.flatten(new Object[]{1, 2, 3, 4, new Object[]{5, 6, 7, 8}, 9, 10})
);
}
@Test
public void testMultipleNestedArrays() throws IllegalArgumentException {
Assert.assertArrayEquals(
"Testing multiple nested arrays",
expectedArray,
FlattenIntegerArray.flatten(new Object[]{1, 2, new Object[]{3, 4, new Object[]{5}, 6, 7}, 8, 9, 10})
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionForObjectInArray() throws IllegalArgumentException {
FlattenIntegerArray.flatten(
new Object[]{new Object()}
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionForObjectInNestedArray() throws IllegalArgumentException {
FlattenIntegerArray.flatten(
new Object[]{1, 2, new Object[]{3, new Object()}}
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionForNullInArray() throws IllegalArgumentException {
FlattenIntegerArray.flatten(
new Object[]{null}
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionForNullInNestedArray() throws IllegalArgumentException {
FlattenIntegerArray.flatten(
new Object[]{1, 2, new Object[]{3, null}}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment