Apache Onami-Test provides a couple of annotations to allow user reusing google-guice modules in test classes.
@org.apache.onami.test.annotation.GuiceModule is a class annotation useful to indicate a list of google-guice modules class.
@org.apache.onami.test.annotation.GuiceProvidedModules is a method annotation usefull to indicate a provider method to create a custom google-guice module.
This example shows a typical use for this annotation; given the following module:
public class SimpleModule
extends AbstractModule
{
@Override
protected void configure()
{
bind( Hello.class ).to( HelloWorld.class );
}
}then users can reuse it specifying
@RunWith( OnamiRunner.class )
@GuiceModules( { SimpleModule.class, AnotherAcmeModule.class } )
public class SimpleTest
{
@com.google.inject.Inject
private Hello helloWorld;
@org.junit.Test
public void testInjectNotStatic()
{
assertNotNull( helloWorld );
assertEquals( "Hello World!!!!", helloWorld.sayHallo() );
}
}GuiceProvidedModule is usefull when your test needs a module that not have a default costructor. So in you testcase you have to declare a public static method with the return type is com.google.inject.Module or Iterable<com.google.inject.Module> or or com.google.inject.Module[]
public class ComplexModule
extends AbstractModule
{
private String name;
public ComplexModule(String name) {
this.name = name;
}
@Override
protected void configure() {
bind( WhoIm.class ).toInstance( new WhoIm(name) );
}
}then, in the test class:
@RunWith( OnamiRunner.class )
public class SimpleTest
{
@GuiceProvidedModules
public static Module createComplexModule()
{
return new ComplexModule( "Marco Speranza" );
}
@com.google.inject.Inject
public static WhoIm whoIm;
@org.junit.Test
public void testWhoIm()
{
assertNotNull( whoIm );
assertEquals( "Marco Speranza", whoIm.sayWhoIm() );
}
}Finally if you want create a module on the fly, your test case should extend com.google.inject.AbstractModule or implement a com.google.inject.Module
@RunWith( OnamiRunner.class )
public class SimpleTest
extends AbstractModule
{
@Override
public void configure()
{
bind( Integer.class ).annotatedWith( Names.named("version") ).toInstance( 10 );
}
@Inject
@Named( "version" )
private Integer version;
@Test
public void testInjectModuleClass()
{
assertNotNull( version );
assertEquals( 10, version.intValue() );
}
}