|
| 1 | +package com.baeldung.java10; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class Java10FeaturesUnitTest { |
| 15 | + |
| 16 | + private List<Integer> someIntList; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setup() { |
| 20 | + someIntList = new ArrayList<>(); |
| 21 | + |
| 22 | + someIntList.add(1); |
| 23 | + someIntList.add(2); |
| 24 | + someIntList.add(3); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenVarInitWithString_thenGetStringTypeVar() { |
| 29 | + var message = "Hello, Java 10"; |
| 30 | + assertTrue(message instanceof String); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenVarInitWithAnonymous_thenGetAnonymousType() { |
| 35 | + var obj = new Object() {}; |
| 36 | + assertFalse(obj.getClass().equals(Object.class)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test(expected = UnsupportedOperationException.class) |
| 40 | + public void whenModifyCopyOfList_thenThrowsException() { |
| 41 | + List<Integer> copyList = List.copyOf(someIntList); |
| 42 | + copyList.add(4); |
| 43 | + } |
| 44 | + |
| 45 | + @Test(expected = UnsupportedOperationException.class) |
| 46 | + public void whenModifyToUnmodifiableList_thenThrowsException() { |
| 47 | + List<Integer> evenList = someIntList.stream() |
| 48 | + .filter(i -> i % 2 == 0) |
| 49 | + .collect(Collectors.toUnmodifiableList()); |
| 50 | + evenList.add(4); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenListContainsInteger_OrElseThrowReturnsInteger() { |
| 55 | + Integer firstEven = someIntList.stream() |
| 56 | + .filter(i -> i % 2 == 0) |
| 57 | + .findFirst() |
| 58 | + .orElseThrow(); |
| 59 | + is(firstEven).equals(Integer.valueOf(2)); |
| 60 | + } |
| 61 | +} |
0 commit comments