Skip to content

Instantly share code, notes, and snippets.

@yurii-litvinov
Last active December 7, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurii-litvinov/2472960a5094799a80d0a0b3664ff552 to your computer and use it in GitHub Desktop.
Save yurii-litvinov/2472960a5094799a80d0a0b3664ff552 to your computer and use it in GitHub Desktop.
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