|
| 1 | +package com.baeldung.generics; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | + |
| 6 | +import java.io.Serializable; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class GenericConstructorUnitTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void givenNonGenericConstructor_whenCreateNonGenericEntry_thenOK() { |
| 15 | + Entry entry = new Entry("sample", 1); |
| 16 | + |
| 17 | + assertEquals("sample", entry.getData()); |
| 18 | + assertEquals(1, entry.getRank()); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void givenGenericConstructor_whenCreateNonGenericEntry_thenOK() { |
| 23 | + Product product = new Product("milk", 2.5); |
| 24 | + product.setSales(30); |
| 25 | + Entry entry = new Entry(product); |
| 26 | + |
| 27 | + assertEquals(product.toString(), entry.getData()); |
| 28 | + assertEquals(30, entry.getRank()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenNonGenericConstructor_whenCreateGenericEntry_thenOK() { |
| 33 | + GenericEntry<String> entry = new GenericEntry<String>(1); |
| 34 | + |
| 35 | + assertNull(entry.getData()); |
| 36 | + assertEquals(1, entry.getRank()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenGenericConstructor_whenCreateGenericEntry_thenOK() { |
| 41 | + GenericEntry<String> entry = new GenericEntry<String>("sample", 1); |
| 42 | + |
| 43 | + assertEquals("sample", entry.getData()); |
| 44 | + assertEquals(1, entry.getRank()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenGenericConstructorWithDifferentType_whenCreateGenericEntry_thenOK() { |
| 49 | + Product product = new Product("milk", 2.5); |
| 50 | + product.setSales(30); |
| 51 | + GenericEntry<Serializable> entry = new GenericEntry<Serializable>(product); |
| 52 | + |
| 53 | + assertEquals(product, entry.getData()); |
| 54 | + assertEquals(30, entry.getRank()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenGenericConstructorWithWildCard_whenCreateGenericEntry_thenOK() { |
| 59 | + Product product = new Product("milk", 2.5); |
| 60 | + product.setSales(30); |
| 61 | + Optional<Product> optional = Optional.of(product); |
| 62 | + GenericEntry<Serializable> entry = new GenericEntry<Serializable>(optional); |
| 63 | + |
| 64 | + assertEquals(product, entry.getData()); |
| 65 | + assertEquals(30, entry.getRank()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK() { |
| 70 | + MapEntry<String, Integer> entry = new MapEntry<String, Integer>("sample", 1); |
| 71 | + |
| 72 | + assertEquals("sample", entry.getKey()); |
| 73 | + assertEquals(1, entry.getValue() |
| 74 | + .intValue()); |
| 75 | + } |
| 76 | +} |
0 commit comments