Skip to content

Commit 60257dd

Browse files
committed
test: add test for recursive constructor
Refs: JF-63
1 parent ec638a8 commit 60257dd

File tree

8 files changed

+44
-10
lines changed

8 files changed

+44
-10
lines changed

src/test/java/com/github/nylle/javafixture/FixtureTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
import com.github.nylle.javafixture.testobjects.TestObjectWithDeepNesting;
99
import com.github.nylle.javafixture.testobjects.TestObjectWithEnumMap;
1010
import com.github.nylle.javafixture.testobjects.TestObjectWithEnumSet;
11-
import com.github.nylle.javafixture.testobjects.TestObjectWithGenericConstructor;
1211
import com.github.nylle.javafixture.testobjects.TestObjectWithGenerics;
1312
import com.github.nylle.javafixture.testobjects.TestObjectWithNestedGenericInterfaces;
1413
import com.github.nylle.javafixture.testobjects.TestObjectWithNestedGenerics;
1514
import com.github.nylle.javafixture.testobjects.TestObjectWithNestedMapsAndLists;
16-
import com.github.nylle.javafixture.testobjects.TestObjectWithoutDefaultConstructor;
1715
import com.github.nylle.javafixture.testobjects.TestPrimitive;
1816
import com.github.nylle.javafixture.testobjects.example.AccountManager;
1917
import com.github.nylle.javafixture.testobjects.example.Contract;
2018
import com.github.nylle.javafixture.testobjects.example.ContractCategory;
2119
import com.github.nylle.javafixture.testobjects.example.ContractPosition;
20+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor;
21+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithoutDefaultConstructor;
2222
import org.junit.jupiter.api.Disabled;
2323
import org.junit.jupiter.api.DisplayName;
2424
import org.junit.jupiter.api.Nested;

src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.github.nylle.javafixture;
22

3-
import com.github.nylle.javafixture.testobjects.TestObjectWithGenericConstructor;
4-
import com.github.nylle.javafixture.testobjects.TestObjectWithPrivateConstructor;
53
import com.github.nylle.javafixture.testobjects.factorymethod.ConstructorExceptionAndFactoryMethod;
64
import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithArgument;
75
import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithGenericArgument;
86
import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithoutArgument;
97
import com.github.nylle.javafixture.testobjects.factorymethod.GenericClassWithFactoryMethodWithoutArgument;
108
import com.github.nylle.javafixture.testobjects.factorymethod.TestObjectWithNonPublicFactoryMethods;
9+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithConstructedField;
10+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor;
11+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithPrivateConstructor;
1112
import org.junit.jupiter.api.DisplayName;
1213
import org.junit.jupiter.api.Nested;
1314
import org.junit.jupiter.api.Test;
@@ -54,7 +55,6 @@ void canCreateInstanceFromConstructor() {
5455
assertThat(result.getValue()).isInstanceOf(String.class);
5556
assertThat(result.getInteger()).isInstanceOf(Optional.class);
5657
}
57-
5858
@Test
5959
@DisplayName("fields not set by constructor are null")
6060
void fieldsNotSetByConstructorAreNull() {
@@ -66,6 +66,18 @@ void fieldsNotSetByConstructorAreNull() {
6666
assertThat(result).isInstanceOf(TestObjectWithGenericConstructor.class);
6767
assertThat(result.getPrivateField()).isNull();
6868
}
69+
@Test
70+
@DisplayName("using constructor is used for all instances")
71+
void usingConstructorIsRecursive() {
72+
73+
var sut = new InstanceFactory(new SpecimenFactory(new Context(Configuration.configure())));
74+
75+
TestObjectWithConstructedField result = sut.construct(fromClass(TestObjectWithConstructedField.class), new CustomizationContext(true));
76+
77+
assertThat(result).isInstanceOf(TestObjectWithConstructedField.class);
78+
assertThat(result.getTestObjectWithGenericConstructor().getPrivateField()).isNull();
79+
assertThat(result.getNotSetByConstructor()).isNull();
80+
}
6981

7082
@Test
7183
@DisplayName("construction will fail if no public constructor is available")

src/test/java/com/github/nylle/javafixture/SpecimenTypeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import com.github.nylle.javafixture.testobjects.TestEnum;
88
import com.github.nylle.javafixture.testobjects.TestObject;
99
import com.github.nylle.javafixture.testobjects.TestObjectGeneric;
10-
import com.github.nylle.javafixture.testobjects.TestObjectWithAllConstructors;
1110
import com.github.nylle.javafixture.testobjects.TestObjectWithGenerics;
1211
import com.github.nylle.javafixture.testobjects.TestObjectWithStaticMethods;
1312
import com.github.nylle.javafixture.testobjects.TestWildCardType;
13+
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithAllConstructors;
1414
import org.junit.jupiter.api.Test;
1515

1616
import java.io.File;

src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/TestObjectWithAllConstructors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nylle.javafixture.testobjects;
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
22

33
public class TestObjectWithAllConstructors {
44
private String value;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
2+
3+
4+
public class TestObjectWithConstructedField {
5+
6+
private String notSetByConstructor;
7+
8+
private final TestObjectWithGenericConstructor testObjectWithGenericConstructor;
9+
10+
11+
public TestObjectWithConstructedField(TestObjectWithGenericConstructor testObjectWithGenericConstructor) {
12+
this.testObjectWithGenericConstructor = testObjectWithGenericConstructor;
13+
}
14+
15+
public TestObjectWithGenericConstructor getTestObjectWithGenericConstructor() {
16+
return testObjectWithGenericConstructor;
17+
}
18+
19+
public String getNotSetByConstructor() {
20+
return notSetByConstructor;
21+
}
22+
}

src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/TestObjectWithGenericConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nylle.javafixture.testobjects;
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
22

33
import java.util.Optional;
44

src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/TestObjectWithPrivateConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nylle.javafixture.testobjects;
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
22

33
public class TestObjectWithPrivateConstructor {
44

src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/TestObjectWithoutDefaultConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.nylle.javafixture.testobjects;
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
22

33
import java.util.List;
44

0 commit comments

Comments
 (0)