Guice

If I use Mockito do I even need Guice?

蹲街弑〆低调 提交于 2019-11-30 14:20:35
问题 I've been learning about Dependency Injection (e.g. Guice) and it seems to me one of the main drivers, testability, is covered already quite nicely by Mocking (e.g. Mockito). Difference between Dependency Injection and Mocking framework (Ninject vs RhinoMock or Moq) is a nice summary of commonality between Dependency Injection and Mockito but it doesn't offer guidance on which to use when they overlap in capability. I'm about to design an API and I'm wondering if I should: A] Use Mockito only

ClassNotFoundException with Guice 2.0

我是研究僧i 提交于 2019-11-30 13:50:13
The code below generates an error using Guice 2.0. With Guice 1.0 everything is fine. The JDK is Java 6 update 15. public class App { public static void main(String[] args) { Guice.createInjector(new AbstractModule() { @Override protected void configure() { // just testing } }); } } The error is: Exception in thread "main" java.lang.NoClassDefFoundError: [Lorg/aopalliance/intercept/MethodInterceptor; at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at java.lang.Class.getDeclaredMethods(Class.java:1791) at com.google.inject

Guice injecting only some of the constructor

空扰寡人 提交于 2019-11-30 13:17:17
问题 Suppose I have some Message class like the following. (This is a made-up class for simplicity.) public class Message { private String text; public Message(String text) { this.text = text; } public void send(Person recipient) { // I think I should be Guice-injecting the sender. MessageSender sender = new EmailBasedMessageSender(); sender.send(recipient, this.text); } } Since I have different MessageSender implementations, and might want to unit test this sending ability, I think I should be

Generics Hell: Can I construct a TypeLiteral<Set<T>> using generics?

最后都变了- 提交于 2019-11-30 12:43:13
问题 The only way I was able to get the below generic method to work was to pass the seemingly redundant TypeLiteral<Set<T>> parameter. I believe it should be possible to construct this parameter programmatically given the other parameter, but can't figure out how. protected <T> Key<Set<T>> bindMultibinder( TypeLiteral<Set<T>> superClassSet, TypeLiteral<T> superClass) { final Key<Set<T>> multibinderKey = Key.get(superClassSet, randomAnnotation); return multibinderKey; } Client code looks like:

Getting multiple guice singletons of the same type

两盒软妹~` 提交于 2019-11-30 11:49:52
can you get 2 singleton instances of the same underlying type? this is obviously trivial in spring as it is based on named instances to which you attach a scope but I can't see the equivalent in guice which is about binding types to implementation classes. Note that I don't want to have to bind to the instance as the instances in question are injected with other dependencies by guice. It's easy in Guice too! Create two biding annotations, say @One and @Two and then bind(MySingleton.class).annotatedWith(One.class).toInstance(new MySingleton()); bind(MySingleton.class).annotatedWith(Two.class)

Can Guice inject Scala objects

六眼飞鱼酱① 提交于 2019-11-30 11:17:14
In Scala, can I use Guice to inject Scala object s? For example, can I inject into s in the following object? object GuiceSpec { @Inject val s: String = null def get() = s } Hbf Some research on Google revealed that you can accomplish this as follows (the code that follows is a ScalaTest unit test): import org.junit.runner.RunWith import org.scalatest.WordSpec import org.scalatest.matchers.MustMatchers import org.scalatest.junit.JUnitRunner import com.google.inject.Inject import com.google.inject.Module import com.google.inject.Binder import com.google.inject.Guice import uk.me.lings

Can Guice automatically create instances of different classes based on a parameter?

不羁的心 提交于 2019-11-30 11:09:59
问题 A standard object factory may look like this: interface I { ... } class A implements I { ... } class B implements I { ... } class IFactory { I getI(int i) { switch (i) { case 1: return new A(); default: return new B(); } } } Is it possible to set up bindings so that switch is done for me, i.e. all I do is call getInstance or inject? I was looking at assisted injection but that seems to be different topic: https://code.google.com/p/google-guice/wiki/AssistedInject 回答1: It sounds like you're

If I use Mockito do I even need Guice?

≯℡__Kan透↙ 提交于 2019-11-30 10:37:53
I've been learning about Dependency Injection (e.g. Guice) and it seems to me one of the main drivers, testability, is covered already quite nicely by Mocking (e.g. Mockito). Difference between Dependency Injection and Mocking framework (Ninject vs RhinoMock or Moq) is a nice summary of commonality between Dependency Injection and Mockito but it doesn't offer guidance on which to use when they overlap in capability. I'm about to design an API and I'm wondering if I should: A] Use Mockito only B] Use Guice and design two implementation of interfaces--one for real and one for testing C] Use

Guice And Scala - Injection on Generics Dependencies

安稳与你 提交于 2019-11-30 09:03:44
I'm trying to create a binding of a generic trait using Guice See how the trait is defined trait Repository[T] See the trait implementation class DomainRepository extends Repository[Domain] My configure method in DomainPersistenceModule is: def configure() { bind(classOf[Repository[Domain]]) .annotatedWith(classOf[DomainDependency]) .to(classOf[DomainRepository]) .in(Scopes.SINGLETON) } The variable whose dependence will be injected is: @Inject @DomainDependency var repository:Repository[Domain] = _ The injection occurs here: val injector:Injector = Guice.createInjector(new PersistenceModule()

How to avoid having injector.createInstance() all over the place when using guice?

你说的曾经没有我的故事 提交于 2019-11-30 08:36:36
There's something I just don't get about guice: According to what I've read so far, I'm supposed to use the Injector only in my bootstrapping class (in a standalone application this would typically be in the main() method), like in the example below (taken from the guice documentation): public static void main(String[] args) { /* * Guice.createInjector() takes your Modules, and returns a new Injector * instance. Most applications will call this method exactly once, in their * main() method. */ Injector injector = Guice.createInjector(new BillingModule()); /* * Now that we've got the injector,