Tag Archives: Mockito

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.