Over a million developers have joined DZone.

Netflix Governator Tests - Introducing governator-junit-runner

DZone's Guide to

Netflix Governator Tests - Introducing governator-junit-runner

· Java Zone
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

Consider a typical Netflix Governator junit test.

public class SampleWithGovernatorJunitSupportTest {

    public LifecycleTester tester = new LifecycleTester();

    public void testExampleBeanInjection() throws Exception {
        Injector injector = tester
                .withBootstrapModule(new SampleBootstrapModule())

        BlogService blogService = injector.getInstance(BlogService.class);
        assertThat(blogService.get(1l), is(notNullValue()));
        assertThat(blogService.getBlogServiceName(), equalTo("Test Blog Service"));


This test is leveraging the Junit rule support provided by Netflix Governator and tests some of the feature sets of Governator - Bootstrap modules, package scanning, configuration support etc.

The test however has quite a lot of boilerplate code which I felt could be reduced by instead leveraging a Junit Runner type model. As a proof of this concept, I am introducing the unimaginatively named project - governator-junit-runner, consider now the same test re-written using this library:

@LifecycleInjectorParams(modules = SampleModule.class, bootstrapModule = SampleBootstrapModule.class, scannedPackages = "sample.gov")
public class SampleGovernatorRunnerTest {

    private BlogService blogService;

    public void testExampleBeanInjection() throws Exception {
        assertEquals("Test Blog Service", blogService.getBlogServiceName());


Most of the boilerplate is now implemented within the Junit runner and the parameters required to bootstrap Governator is passed in through the LifecycleInjectorParams annotation. The test instance itself is a bound component and thus can be injected into, this way the instances which need to be tested can be injected into the test itself and asserted on. If you want more fine-grained control, the LifecycleManager itself can be injected into the test!:

private Injector injector;

private LifecycleManager lifecycleManager;

If this interests you, more samples are at the project site here.

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.


Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}