|
Java example source code file (InjectorSpiTest.java)
The InjectorSpiTest.java Java example source code
package com.google.inject.spi;
import com.google.inject.AbstractModule;
import com.google.inject.Binding;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import junit.framework.TestCase;
import java.util.Map;
/**
* @author sberlin@gmail.com (Sam Berlin)
*/
public class InjectorSpiTest extends TestCase {
public void testExistingBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class);
bind(Baz.class);
}
});
// Sanity check -- ensure we return the proper binding for all existing bindings.
for(Map.Entry<Key>, Binding>> entry : injector.getAllBindings().entrySet()) {
assertSame(entry.getValue(), injector.getExistingBinding(entry.getKey()));
}
// Now run through specifics...
Binding<?> binding;
// 1) non-Provider Foo.class
binding = injector.getExistingBinding(Key.get(Foo.class));
assertNotNull(binding);
assertEquals(Foo.class, binding.getKey().getTypeLiteral().getRawType());
// 2) Provider<Foo> class (should already exist, because Baz @Injects it).
// the assertTrue is a bit stricter than necessary, but makes sure this works for pre-existing Provider bindings
assertTrue(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider
Other Java examples (source code examples)Here is a short list of links related to this Java InjectorSpiTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.