JDave Tutorial

If you have not read my JDave Tutorial, here’s all posts. JDave is a BDD testing frameworkJDave which gives developers insight understanding what going on in their code. It includes basics, mocking and dependency injection.

JDave BDD Testing Tutorial Part 1: Basics
JDave BDD Testing Tutorial Part 2: Mockito
JDave BDD Testing Tutorial Part 3: Google Guice

Google Guice with Easy Environment Resolving

Google GuiceWorking with Google Guice is serious fun because of easy of usage and lightness. There still remains one problem when working in environment with multiple servers. Usually one Guice module is not enough and you may want to use different implementations. This is usually case when you want to have all external services stubbed, so you can fire server up without any network connections.

Google Guice Example

This snip of code fetches environment from system properties and instatiates Guice Injector with correct modules for the environment.

Examples can be pulled/viewed from GitHub: Guice with Easy Environment Resolving

Maven pom configuration

Automatic running environment dependencies

First we define annotation that is going to be used in Guice Module to define to which environment particular module belongs.

Annotation is used to indicate environment in Guice Module.

Environment revolving iterates all configured modules and selects proper modules depending on annotation value.

EnvironmentResolver uses System properties to define in which environment current server is. Implementing interface we can define multiple ways to fetch environment property.

In this example -Denvironment=DEVELOPMENT is given from command line and DevelopmentModule will be loaded, because we have @Environment("DEVELOPMENT") annotated in particular class.

Here’s class that should be called when server is starting up. This can be done by implementing ServletContextListener. Out of web container you can just invoke in main class.

After resolving, we can instantiate injector and set it to InjectorHolder. This can be used in application to fetch injector.

JDave BDD Testing Tutorial Part 3: Google Guice

Google GuiceGoogle Guice is a dependency injection framework that is lightweight and easy to use. It helps not only in application,  but tests to achieve loose coupling. JDave BDD framework has no problem using Google Guice for flexible testing. This is Guice tutorial for basic usages in JDave BDD testing.

If you haven’t read Part 1 and 2, I suggest reading them:
JDave BDD Testing Tutorial Part 1: Basics
JDave BDD Testing Tutorial Part 2: Mockito

JDave BDD Example

Examples can be pulled/viewed from GitHub: JdaveBDDExamples

Maven pom configuration

We need to add Guice as dependency.

Service Layer

We start of with MmlOneLinerMaiscService which is same than in previous post, but we no longer need protected method. Instead we use @Inject annotation to inject implementation. We also tell to Guice that use telnet implementation of client. We do this by calling @Named("telnet"). There’s also main-method included to, that we can test that it really works. We need to create Injector which then holds all bindings.

Guice Module

Here’s our module that defines our Guice bindings. Here we see usage of annotatedWith, toInstance and to. MmlModule is used in our MmlOneLinerMaiscService main-method.

Specification

Here we see modified test, that uses Guice to bind implementations. In create-method we instantiate injector, assign our mock implementations to it. After that we getInstance from guice. Easy!