-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGenericClassCreationTest.java
64 lines (47 loc) · 2.31 KB
/
GenericClassCreationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package by.andd3dfx.core;
import by.andd3dfx.core.GenericClassCreation.CreatorUsingDeclaredConstructor;
import by.andd3dfx.core.GenericClassCreation.CreatorUsingSupplier;
import lombok.AllArgsConstructor;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
public class GenericClassCreationTest {
@Test
public void createStringBy_CreatorUsingDeclaredConstructor() {
CreatorUsingDeclaredConstructor<String> container = new CreatorUsingDeclaredConstructor<>();
assertThat(container.createObject(String.class)).isEqualTo("");
}
@Test
public void createStringBy_CreatorUsingSupplier() {
CreatorUsingSupplier<String> container = new CreatorUsingSupplier<>(String::new);
assertThat(container.createObject()).isEqualTo("");
}
@Test
public void createClassWithoutFieldsBy_CreatorUsingDeclaredConstructor() {
CreatorUsingDeclaredConstructor<CustomClassWithoutFields> container = new CreatorUsingDeclaredConstructor<>();
assertThat(container.createObject(CustomClassWithoutFields.class)).isInstanceOf(CustomClassWithoutFields.class);
}
@Test
public void createClassWithoutFieldsBy_CreatorUsingSupplier() {
CreatorUsingSupplier<CustomClassWithoutFields> container = new CreatorUsingSupplier<>(CustomClassWithoutFields::new);
assertThat(container.createObject()).isInstanceOf(CustomClassWithoutFields.class);
}
@Test
public void createClassWithFieldBy_CreatorUsingDeclaredConstructor() {
CreatorUsingDeclaredConstructor<CustomClassWithField> container = new CreatorUsingDeclaredConstructor<>();
assertThrows(NoSuchMethodException.class, () -> {
container.createObject(CustomClassWithField.class);
});
}
@Test
public void createClassWithFieldBy_CreatorUsingSupplier() {
CreatorUsingSupplier<CustomClassWithField> container = new CreatorUsingSupplier<>(() -> new CustomClassWithField(0));
assertThat(container.createObject()).isInstanceOf(CustomClassWithField.class);
}
public static class CustomClassWithoutFields {
}
@AllArgsConstructor
public static class CustomClassWithField {
private int value;
}
}