Skip to content

Instantly share code, notes, and snippets.

@rozaydin
Created April 13, 2017 08:29
Show Gist options
  • Save rozaydin/b906105ed0eb8aaae177cb1eb2b7d0f2 to your computer and use it in GitHub Desktop.
Save rozaydin/b906105ed0eb8aaae177cb1eb2b7d0f2 to your computer and use it in GitHub Desktop.
JUnit Tests that show fixed-size list is unmutable and add, remove, clear methods throw UnsupportedOperationException
package org.mshowto;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
public class arraysaslist {
private Integer[] numbers;
@Before
public void initArray() {
// 1. create a sample numbers array
numbers = new Integer[100];
IntStream.range(0, 100).forEach((elem) -> {
numbers[elem] = elem;
});
}
@Test(expected = UnsupportedOperationException.class)
public void testAdd() {
// 1. convert the array to fixed-size list
List<Integer> numbersFixedSizeList = Arrays.asList(numbers);
// invoke add method (UnsupportedOperationException shall be thrown)
numbersFixedSizeList.add(578);
}
@Test(expected = UnsupportedOperationException.class)
public void testRemove() {
// 1. convert the array to fixed-size list
List<Integer> numbersFixedSizeList = Arrays.asList(numbers);
// invoke remove method (UnsupportedOperationException shall be thrown)
numbersFixedSizeList.remove(0);
}
@Test(expected = UnsupportedOperationException.class)
public void testClear() {
// 1. convert the array to fixed-size list
List<Integer> numbersFixedSizeList = Arrays.asList(numbers);
// invoke clear method (UnsupportedOperationException shall be thrown)
numbersFixedSizeList.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment