|
1 | 1 | package by.andd3dfx.core;
|
2 | 2 |
|
| 3 | +import by.andd3dfx.core.GenericClassCreation.CreatorUsingDeclaredConstructor; |
| 4 | +import by.andd3dfx.core.GenericClassCreation.CreatorUsingSupplier; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import static org.hamcrest.CoreMatchers.instanceOf; |
3 | 9 | import static org.hamcrest.CoreMatchers.is;
|
4 | 10 | import static org.hamcrest.MatcherAssert.assertThat;
|
5 |
| - |
6 |
| -import by.andd3dfx.core.GenericClassCreation.SomeContainer1; |
7 |
| -import by.andd3dfx.core.GenericClassCreation.SomeContainer2; |
8 |
| -import java.lang.reflect.InvocationTargetException; |
9 |
| -import org.junit.Test; |
| 11 | +import static org.junit.Assert.assertThrows; |
10 | 12 |
|
11 | 13 | public class GenericClassCreationTest {
|
12 | 14 |
|
13 | 15 | @Test
|
14 |
| - public void createObject1() |
15 |
| - throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
16 |
| - SomeContainer1<String> someContainer1 = new SomeContainer1<>(); |
| 16 | + public void createStringBy_CreatorUsingDeclaredConstructor() { |
| 17 | + CreatorUsingDeclaredConstructor<String> container = new CreatorUsingDeclaredConstructor<>(); |
| 18 | + |
| 19 | + assertThat(container.createObject(String.class), is("")); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void createStringBy_CreatorUsingSupplier() { |
| 24 | + CreatorUsingSupplier<String> container = new CreatorUsingSupplier<>(String::new); |
| 25 | + |
| 26 | + assertThat(container.createObject(), is("")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void createClassWithoutFieldsBy_CreatorUsingDeclaredConstructor() { |
| 31 | + CreatorUsingDeclaredConstructor<CustomClassWithoutFields> container = new CreatorUsingDeclaredConstructor<>(); |
| 32 | + |
| 33 | + assertThat(container.createObject(CustomClassWithoutFields.class), instanceOf(CustomClassWithoutFields.class)); |
| 34 | + } |
17 | 35 |
|
18 |
| - String createdObject = someContainer1.createObject(String.class); |
| 36 | + @Test |
| 37 | + public void createClassWithoutFieldsBy_CreatorUsingSupplier() { |
| 38 | + CreatorUsingSupplier<CustomClassWithoutFields> container = new CreatorUsingSupplier<>(CustomClassWithoutFields::new); |
19 | 39 |
|
20 |
| - assertThat("Empty string expected", createdObject, is("")); |
| 40 | + assertThat(container.createObject(), instanceOf(CustomClassWithoutFields.class)); |
21 | 41 | }
|
22 | 42 |
|
23 | 43 | @Test
|
24 |
| - public void createObject2() { |
25 |
| - SomeContainer2<String> someContainer2 = new SomeContainer2<>(String::new); |
| 44 | + public void createClassWithFieldBy_CreatorUsingDeclaredConstructor() { |
| 45 | + CreatorUsingDeclaredConstructor<CustomClassWithField> container = new CreatorUsingDeclaredConstructor<>(); |
| 46 | + |
| 47 | + assertThrows(NoSuchMethodException.class, () -> { |
| 48 | + container.createObject(CustomClassWithField.class); |
| 49 | + }); |
| 50 | + } |
26 | 51 |
|
27 |
| - String createdObject = someContainer2.createObject(); |
| 52 | + @Test |
| 53 | + public void createClassWithFieldBy_CreatorUsingSupplier() { |
| 54 | + CreatorUsingSupplier<CustomClassWithField> container = new CreatorUsingSupplier<>(() -> new CustomClassWithField(0)); |
| 55 | + |
| 56 | + assertThat(container.createObject(), instanceOf(CustomClassWithField.class)); |
| 57 | + } |
| 58 | + |
| 59 | + public static class CustomClassWithoutFields { |
| 60 | + } |
28 | 61 |
|
29 |
| - assertThat("Empty string expected", createdObject, is("")); |
| 62 | + @AllArgsConstructor |
| 63 | + public static class CustomClassWithField { |
| 64 | + private int value; |
30 | 65 | }
|
31 | 66 | }
|
0 commit comments