diff --git a/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionInstancesTest.java b/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionInstancesTest.java index 9659814..086c679 100644 --- a/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionInstancesTest.java +++ b/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionInstancesTest.java @@ -1,5 +1,6 @@ package org.panda_lang.utilities.inject; +import java.security.InvalidParameterException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -31,6 +32,7 @@ void shouldNotInjectInstances() { }); }); + assertThrows(InvalidParameterException.class, () -> injector.forConstructor(InvalidClass.class).newInstance(), "Class has contain one and only one constructor"); assertThrows(DependencyInjectionException.class, () -> injector.newInstance(Service.class)); } @@ -46,4 +48,15 @@ public Service(Bean bean, Custom custom) { } } + private static class InvalidClass { // 2 constructors (only 1 is allowed) + public InvalidClass(Bean bean, Custom custom) { + assertNotNull(bean); + assertNotNull(custom); + } + + public InvalidClass(Bean bean) { + assertNotNull(bean); + } + } + } diff --git a/di/src/test/java/org/panda_lang/utilities/inject/MethodsInvocationTest.java b/di/src/test/java/org/panda_lang/utilities/inject/MethodsInvocationTest.java new file mode 100644 index 0000000..610fa66 --- /dev/null +++ b/di/src/test/java/org/panda_lang/utilities/inject/MethodsInvocationTest.java @@ -0,0 +1,72 @@ +package org.panda_lang.utilities.inject; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MethodsInvocationTest { + + private static class TestClass { + + private String testString; + private int testInt = 0; + + public void testMethod() { + this.testString = "test"; + } + + @TestAnnotation + public void annotatedMethod() { + this.testInt += 2; + } + + @TestAnnotation + public void annotatedMethod(int value) { + this.testInt *= value; + } + + @TestAnnotation2 + public void annotatedMethod2() { + throw new IllegalStateException("This method should not be invoked"); + } + + public void failMethod() { + throw new RuntimeException(); + } + + } + + @Retention(RetentionPolicy.RUNTIME) + private @interface TestAnnotation { } + + @Retention(RetentionPolicy.RUNTIME) + private @interface TestAnnotation2 {} + + @Test + void shouldInvokeMethods() throws NoSuchMethodException { + Injector injector = DependencyInjection.createInjector(resources -> { + resources.on(int.class).assignInstance(3); + }); + + TestClass testClass = new TestClass(); + injector.invokeMethod(TestClass.class.getMethod("testMethod"), testClass); + assertEquals("test", testClass.testString); + + injector.invokeAnnotatedMethods(TestAnnotation.class, testClass); + assertEquals(6, testClass.testInt); + } + + @Test + void shouldNotInvokeMethods() throws NoSuchMethodException { + Injector injector = DependencyInjection.createInjector(resources -> { + resources.on(int.class).assignInstance(3); + }); + + TestClass testClass = new TestClass(); + assertThrows(DependencyInjectionException.class, () -> injector.invokeMethod(TestClass.class.getMethod("failMethod"), testClass)); + } + +}