Skip to content

Commit 744aec6

Browse files
Merge pull request #73 from AutomateThePlanet/object-factory-fix
ObjectFactory Fix for No Args Constructor
2 parents e8fa6d3 + ae19a90 commit 744aec6

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/ObjectFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ private static <T> Constructor<T> getSuitableConstructor(Class<T> clazz, Object[
3737
var argumentTypes = getArgumentTypes(arguments);
3838

3939
try {
40-
return (Constructor<T>)findConstructorMatch(clazz, argumentTypes);
40+
return findConstructorMatch(clazz, argumentTypes);
4141
} catch (NoSuchMethodException e) {
42+
if (argumentTypes == null || argumentTypes.length == 0) {
43+
throw new ConstructorNotFoundException();
44+
}
45+
4246
var types = new StringBuilder();
4347
for (var type : argumentTypes) {
4448
types.append(type.getName() + System.lineSeparator());
@@ -73,7 +77,7 @@ private static <T> Constructor<T> findVarArgsConstructor(Class clazz, Class[] ar
7377
args[args.length - 1] = getVarArgsType(clazz, argumentTypes);
7478

7579
return clazz.getDeclaredConstructor(args);
76-
} catch (NoSuchMethodException ignored) {
80+
} catch (NoSuchMethodException|NullPointerException ignored) {
7781
}
7882

7983
throw new NoSuchMethodException("No matching constructor found for the provided argument types.");
@@ -113,7 +117,9 @@ public ConstructorNotFoundException(int numberOfTypes, String types) {
113117
Given argument types:\n%s
114118
""").formatted(numberOfTypes, types));
115119
}
120+
116121
public ConstructorNotFoundException() {
122+
super("No default constructor was found." + System.lineSeparator());
117123
}
118124

119125
public ConstructorNotFoundException(String message) {

framework-tests/bellatrix.core.tests/src/test/java/factory/InstanceFactoryTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package factory;
22

3+
import factory.data.Boss;
34
import factory.data.Employee;
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.Test;
67
import solutions.bellatrix.core.plugins.junit.BaseTest;
78
import solutions.bellatrix.core.utilities.InstanceFactory;
9+
import solutions.bellatrix.core.utilities.ObjectFactory;
810

911
public class InstanceFactoryTests extends BaseTest {
1012
@Test
@@ -28,7 +30,12 @@ public void objectReturned_When_UsedCustomArgsConstructor() {
2830
}
2931

3032
@Test
31-
public void objectNotReturned_When_UsedNonExistentConstructor() {
33+
public void returnedNull_When_TriedUsingNonExistentConstructor() {
3234
Assertions.assertNull(InstanceFactory.create(Employee.class, "John Doe"));
3335
}
36+
37+
@Test
38+
public void returnedNull_When_TriedUsingNonExistentNoArgsConstructor() {
39+
Assertions.assertNull(InstanceFactory.create(Boss.class));
40+
}
3441
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package factory.data;
2+
3+
public class Boss {
4+
public Boss(String firstName, String lastName, String businessEmail) {
5+
this.firstName = firstName;
6+
this.lastName = lastName;
7+
this.businessEmail = businessEmail;
8+
}
9+
10+
public Boss(String firstName, String lastName, String businessEmail, String personalEmail, Object[] additionalData) {
11+
this.firstName = firstName;
12+
this.lastName = lastName;
13+
this.businessEmail = businessEmail;
14+
this.personalEmail = personalEmail;
15+
this.additionalData = additionalData;
16+
}
17+
18+
public String firstName;
19+
public String lastName;
20+
public String businessEmail;
21+
public String personalEmail;
22+
public Object[] additionalData;
23+
}

0 commit comments

Comments
 (0)