Skip to content

Commit

Permalink
Add methods invocation test
Browse files Browse the repository at this point in the history
  • Loading branch information
P3ridot committed Jul 18, 2023
1 parent 11628bb commit 0ad197b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 0ad197b

Please sign in to comment.