Arquillian Google Guice Extension 1.0.0.Alpha1 Released

The Arquillian team is proud to announce the 1.0.0.Alpha1 release of the Arquillian Google Guice Extension component!

This is the first release of the Arquillian Guice extension. We’re bringing the next IOC container into the Arquillian universe.

Some of the highlights in this release

Dependency injection

Using this extension setting up a Guice test becomes pretty simple, allowing you to inject all Guice managed dependencies directly into the test case.

Built in Guice deployer

With the help of this extension you don’t need to add Guice to your deployments manually, the extension will take care of that for you.

There are two ways of enabling Guice injection in an Arquillian test case:
The first way is to annotate an Arquillian test case with @GuiceConfiguration. The annotation values allow you to specify the modules that will be passed to the injector instance on creation.

Example test:

AppointmentServiceImplTestCase.java
@RunWith(Arquillian.class)
@GuiceConfiguration(AppointmentModule.class)
public class AppointmentServiceImplTestCase {

   @Deployment
   public static JavaArchive createTestArchive() {
       return ShrinkWrap.create(JavaArchive.class, "guice-test.jar")
               .addClasses(Appointment.class,
                       AppointmentRepository.class, AppointmentRepositoryImpl.class,
                       AppointmentService.class, AppointmentServiceImpl.class,
                       AppointmentModule.class);
   }

   @Inject
   @Named("appointmentService")
   private AppointmentService appointmentService;

   @Test
   public void testGetAll() {

       Appointment appointment1 = createAppointment("Important", "Work", new Date());
       Appointment appointment2 = createAppointment("Do not forget", "Work", new Date());

       appointmentService.add(appointment1);
       appointmentService.add(appointment2);

       List<Appointment> result = appointmentService.getAll();
       assertNotNull("Method returned null.", result);
       assertEquals("Invalid element count, 2 appointments were expected.", 2, result.size());
   }
}

Where AppointmentModule is defined as fallows:

AppointmentModule.java
public class AppointmentModule implements Module {

    public void configure(Binder binder) {

        binder.bind(AppointmentRepository.class)
                .annotatedWith(Names.named("appointmentRepository"))
                .to(AppointmentRepositoryImpl.class);
        binder.bind(AppointmentService.class)
                .annotatedWith(Names.named("appointmentService"))
                .to(AppointmentServiceImpl.class);
    }
}

Guice configuration uses module instances instead of static classes known at compile time, which means that modules can have different state at runtime. Taking that into consideration, static annotation could in some cases be limiting. This is way we added a second possibility for enabling Guice. A static factory method could be added in the test case and annotated with @GuiceInjector that instantiates the Injector directly. The extension will invoke the method prior to test execution and based on the returned injector the dependencies will be available for injection. Is the previous test we would only need to add:

@GuiceInjector
public static Injector createInjector() {

    return Guice.createInjector(new AppointmentModule());
}

The extension defines some pretty simple settings to control how the packaging occur; specify the version of guice to package and if the auto packaging should happen at all.

arquillian.xml
<extension qualifier="guice">
     <property name="autoPackage">false</property>
     <property name="guiceVersion">3.0</property>
</extension>

We look forward to hearing your feedback about this release in the community forums!

What is Arquillian?

Arquillian is open source software that empowers you to test JVM-based applications more effectively. Created to defend the software galaxy from bugs, Arquillian brings your test to the runtime so you can focus on testing your application's behavior rather than managing the runtime. Using Arquillian, you can develop a comprehensive suite of tests from the convenience of your IDE and run them in any IDE, build tool or continuous integration environment.

Release details

Component Arquillian Google Guice Extension
Version 1.0.0.Alpha1 view tag
Release date 2013-06-19
Released by Aslak Knutsen
Compiled against

Published artifacts org.jboss.arquillian.extension

  • org.jboss.arquillian.extension » arquillian-guice-api jar pom
  • org.jboss.arquillian.extension » arquillian-guice-impl jar pom
  • org.jboss.arquillian.extension » arquillian-guice-bom pom

Release notes and resolved issues 1

Initial release of Arquillian Google Guice Extension

Thanks to the following list of contributors: Jakub Narloch, Aslak Knutsen