Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package task;
public class AmbiguousImplementationException extends Exception {
}
package task.testClasses;
public class ClassWithOneClassDependency {
public final ClassWithoutDependencies dependency;
public ClassWithOneClassDependency(ClassWithoutDependencies dependency) {
this.dependency = dependency;
}
}
package task.testClasses;
public class ClassWithOneInterfaceDependency {
public final Interface dependency;
public ClassWithOneInterfaceDependency(Interface dependency) {
this.dependency = dependency;
}
}
package task.testClasses;
public class ClassWithoutDependencies {
}
package task;
public class ImplementationNotFoundException extends Exception {
}
package task;
public class InjectionCycleException extends Exception {
}
package task;
import java.util.List;
public class Injector {
/**
* Create and initialize object of `rootClassName` class using classes from
* `implementationClassNames` for concrete dependencies.
*/
public static Object initialize(String rootClassName, List<String> implementationClassNames) throws Exception {
throw new IllegalStateException();
}
}
package task.testClasses;
public interface Interface {
}
package task.testClasses;
public class InterfaceImpl implements Interface {
}
package task;
import org.junit.Test;
import task.testClasses.ClassWithOneClassDependency;
import task.testClasses.ClassWithOneInterfaceDependency;
import task.testClasses.ClassWithoutDependencies;
import task.testClasses.InterfaceImpl;
import java.util.Collections;
import static org.junit.Assert.assertTrue;
public class TestInjector {
@Test
public void injectorShouldInitializeClassWithoutDependencies()
throws Exception {
Object object = Injector.initialize("task.testClasses.ClassWithoutDependencies", Collections.emptyList());
assertTrue(object instanceof ClassWithoutDependencies);
}
@Test
public void injectorShouldInitializeClassWithOneClassDependency()
throws Exception {
Object object = Injector.initialize(
"task.testClasses.ClassWithOneClassDependency",
Collections.singletonList("task.testClasses.ClassWithoutDependencies")
);
assertTrue(object instanceof ClassWithOneClassDependency);
ClassWithOneClassDependency instance = (ClassWithOneClassDependency) object;
assertTrue(instance.dependency != null);
}
@Test
public void injectorShouldInitializeClassWithOneInterfaceDependency()
throws Exception {
Object object = Injector.initialize(
"task.testClasses.ClassWithOneInterfaceDependency",
Collections.singletonList("task.testClasses.InterfaceImpl")
);
assertTrue(object instanceof ClassWithOneInterfaceDependency);
ClassWithOneInterfaceDependency instance = (ClassWithOneInterfaceDependency) object;
assertTrue(instance.dependency instanceof InterfaceImpl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.