Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Field Injection When Mocking Frameworks Fail

DZone's Guide to

Field Injection When Mocking Frameworks Fail

Learn more about field injection with mocking frameworks, and CDI in applications when unit testing Java classes.

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

Challenge

You are using Dependency Injection (CDI) in your application and you want to unit test your Java classes without making it an integration test by using Weld or Arquillian. You are using a Mocking framework like Mockito or EasyMock but still have trouble getting all your dependencies injected into the class because one of the injections is a String type or another final class.

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class java.lang.String
Mockito cannot mock/spy following:
   - final classes
   - anonymous classes
   - primitive types

I personally came by this challenge after writing a couple of @Producer methods for injecting a property from a property file. So in effect I was injecting a String and I had trouble writing a test for it.

Solution

Create your own small injection utility method:

  public static void injectField(final Object injectable, 
                                 final String fieldname, 
                                 final Object value) {
      try {
          final java.lang.reflect.Field field = injectable.getClass()
                                                          .getDeclaredField(fieldname);
          final boolean origionalValue = field.isAccessible();
          field.setAccessible(true);
          field.set(injectable, value);
          field.setAccessible(origionalValue);
      } catch (final NoSuchFieldException | IllegalAccessException e) {
          throw new RuntimeException(e.getMessage(), e);
      }
  }

So if you have a class like this, with an @Inject and no setter, you want to test:

package nl.ivonet.example;
import javax.inject.Inject;
public class Greeting {
    @Inject
    private String greeting;

    @Override
    public String toString() {
        return this.greeting;
    }
}

You can test it like this:

package nl.ivonet.example;
import org.junit.Test;
import static nl.ivonet.example.utility.CDI.injectField;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class GreetingTest {
    @Test
    public void testToString() throws Exception {
        Greeting greeting = new Greeting();
        injectField(greeting, "greeting", "Hello, world");
        assertThat(greeting.toString(), is("Hello, world"));
    }
}

For other Injectables, you can still use Mockito or your Mocking framework of choice but for final classes you can do the above.

Have fun,

Ivo.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:
java ,junit ,mock final class ,mock

Published at DZone with permission of Ivo Woltring. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}