Tag Archives: JDave

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!

JDave BDD Testing Tutorial Part 2: Mockito

mockitoMockito is simple mocking framework that let’s coder concentrate into testing. Written Mockito is very readable and generates meaningful error messages. Combined with JDave BDD framework, it becomes one great tool.

If you haven’t read Part 1, I suggest reading it: JDave BDD Testing Tutorial Part 1

JDave BDD Example

Examples can be pulled/viewed from GitHub: JdaveBDDExamples

We will continue with same code than in part 1.

Maven pom configuration

We need only one additional dependency:

Service Layer

This interface for our service, let’s call it MmlService

Here’s concrete implementation of our service. Let’s call it MmlOneLinerService It takes one parameter in constructor and that is client interface. This way we can have multiple carrier protocols. If you checkout example code, there’s couple of concrete implementations to MmlClient.

Specification

Here’s the beef. We start by creating normal JDave specification: MmlOneLinerServiceSpec. We have two member variables for classes MmlClient and MmlCreator. They’re both mocks. Familiar create-method is there, but this time we’re making anonymous override to our service. We return our mock to service.

We want to make sure that message get’s delivered without any modifications. First we use Mockito.when().thenReturn(); to define what a mock returns. We really do not care about MmlCreator since it’s already tested. Then we execute context.sendMessage(). After this we want to make sure that message did not change. This is made with Mockito.verify().method(). It can be called only to mocks.

That was basic usage of Mockito mocking framework. Mockito has many more features including mocking individual methods. Next time we’ll add Guice dependency injection framework. We still have some extra methods in our service because of testing and we want to get rid of them.

Remember 101 of testing: first red light, then green.

JDave BDD Testing Tutorial Part 1: Basics

JDaveJDave is excellent “testing” framework for Behavior Driven Development “testing”. It uses JUnit adapter launch specifications. Yes. Specifications. They’re not called test, but specifications. This is because given class specification can be read from these. If written correctly, they’re best documentation to your code.

Often with difficult business cases you can write skeleton specification and give result to PO. Result can be viewed together and read in clear sentences that given task is understood correctly. This is because all class and methods names are meaningful with correct scope. Example:

JDave BDD Example

Examples can be pulled/viewed from GitHub: JdaveBDDExamples

Maven pom.xml configuration

JDave comes packed in handy maven dependency:

We also need to configure Maven Surefire plugin to understand our tests.

Doman object

First we have domain class which holds basic information of a customer:

Business code

Next we have some business code that generates “Man-machine language” (-ish).

Specification

We want to describe MmlCreator class. We call it MmlCreatorSpec.

JDave has it’s own runner for JUnit: JDaveRunner.class. It’s given @RunWithannotation.
We must extend jdave.Specification<T> and give class to specify as generic type.

We start code by creating Any class which holds only one method: create. It’s job is to create instance of class in hand to Runner. JDave invokes all public inner classes that have create-method. After this we can access our instance by invoking variable context. After create method, all public methods will be invoked in public inner class.

WhenCreatingMllMessageWithInvalidData extends Any and inherits create-method. In this context (inner class), we have only one “test”. It checks that business method createFrom throws RuntimeException. Exception type tests are run in Block which isolates runnable code.

WhenCreatingMllMessageWithValidData extends Any and inherits create-method. In this context (inner class), we have only one “test”. It checks that busines methodcreateFrom generates valid MML message.

BDD sentences:

JDave supports Hamcrest matchers and comes with JMock2 mocking library.

In the next parts of this tutorial, I’ll be covering basic usage of Mockito mocking library and Guice dependency injection framework in JDave BDD specifications.