Skip to content

Commit 3c5fb59

Browse files
committed
Add ConstructorUtilsExamples
1 parent 1214e8a commit 3c5fb59

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.robertsmieja.example.apache.commons.lang3;
2+
3+
import org.apache.commons.lang3.reflect.ConstructorUtils;
4+
import org.junit.Test;
5+
6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.InvocationTargetException;
8+
9+
import static org.junit.Assert.assertNotNull;
10+
11+
public class ConstructorUtilsExamples {
12+
13+
public static class CoolClassWithConstructors {
14+
public CoolClassWithConstructors(){}
15+
public CoolClassWithConstructors(String stringArg){}
16+
public CoolClassWithConstructors(int intArg) {}
17+
}
18+
19+
20+
@Test
21+
public void constructorUtilsExample() throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
22+
Constructor<CoolClassWithConstructors> emptyCosntructor = ConstructorUtils.getAccessibleConstructor(CoolClassWithConstructors.class);
23+
Constructor<CoolClassWithConstructors> stringConstructor = ConstructorUtils.getAccessibleConstructor(CoolClassWithConstructors.class, String.class);
24+
Constructor<CoolClassWithConstructors> intConstructor = ConstructorUtils.getAccessibleConstructor(CoolClassWithConstructors.class, int.class);
25+
26+
assertNotNull(emptyCosntructor);
27+
assertNotNull(stringConstructor);
28+
assertNotNull(intConstructor);
29+
30+
CoolClassWithConstructors instance = emptyCosntructor.newInstance();
31+
assertNotNull(instance);
32+
33+
CoolClassWithConstructors emptyConstructorInstance = ConstructorUtils.invokeConstructor(CoolClassWithConstructors.class);
34+
CoolClassWithConstructors stringConstructorInstance = ConstructorUtils.invokeConstructor(CoolClassWithConstructors.class, "Argument");
35+
CoolClassWithConstructors intConstructorInstance = ConstructorUtils.invokeConstructor(CoolClassWithConstructors.class, 1);
36+
37+
assertNotNull(emptyConstructorInstance);
38+
assertNotNull(stringConstructorInstance);
39+
assertNotNull(intConstructorInstance);
40+
}
41+
}

0 commit comments

Comments
 (0)