Injecting a String property with @InjectMocks

40,709

Solution 1

You cannot do this with Mockito, because, as you mentioned yourself, a String is final and cannot be mocked.

There is a @Spy annotation which works on real objects, but it has the same limitations as @Mock, thus you cannot spy on a String.

There is no annotation to tell Mockito to just inject that value without doing any mocking or spying. It would be a good feature, though. Perhaps suggest it at the Mockito Github repository.

You will have to manually instantiate your controller if you don't want to change your code.

The only way to have a pure annotation based test is to refactor the controller. It can use a custom object that just contains that one property, or perhaps a configuration class with multiple properties.

@Component
public class MyProperty {

    @Value("${my.property}")
    private String myProperty;

    ...
}

This can be injected into the controller.

@Autowired
public AbcController(XyzService xyzService, MyProperty myProperty) { 
    ... 
}

You can mock and inject this then.

@RunWith(MockitoJUnitRunner.class)
public class AbcControllerTest {

    @Mock
    private XyzService mockXyzService;

    @Mock
    private MyProperty myProperty;

    @InjectMocks
    private AbcController controllerUnderTest;

    @Before
    public void setUp(){
        when(myProperty.get()).thenReturn("my property value");
    }

    /* tests */
}

This is not pretty straight forward, but at least you will be able to have a pure annotation based test with a little bit of stubbing.

Solution 2

You can't do this with Mockito, but Apache Commons actually has a way to do this using one of its built in utilities. You can put this in a function in JUnit that is run after Mockito injects the rest of the mocks but before your test cases run, like this:

@InjectMocks
MyClass myClass;

@Before
public void before() throws Exception {
    FieldUtils.writeField(myClass, "fieldName", fieldValue, true);
}

Solution 3

Since you're using Spring, you can use the org.springframework.test.util.ReflectionTestUtils from the spring-test module. It neatly wraps setting a field on a object or a static field on a class (along with other utility methods).

@RunWith(MockitoJUnitRunner.class)
public class AbcControllerTest {

    @Mock
    private XyzService mockXyzService;

    @InjectMocks
    private AbcController controllerUnderTest;

    @Before
    public void setUp() {
        ReflectionTestUtils.setField(controllerUnderTest, "myProperty", 
               "String you want to inject");
    }

    /* tests */
}

Solution 4

Just don't use @InjectMocks in that case.

do:

@Before
public void setup() {
 controllerUnderTest = new AbcController(mockXyzService, "my property value");
}

Solution 5

Solution is simple: You should put constructor injection for the object type while for primitive/final dependencies you can simply use setter injection and that'll be fine for this scenario.

So this:

public AbcController(XyzService xyzService, @Value("${my.property}") String myProperty) {/*...*/}

Would be changed to:

@Autowired
public AbcController(XyzService xyzService) {/*...*/}

@Autowired
public setMyProperty(@Value("${my.property}") String myProperty){/*...*/}

And the @Mock injections in test would be as simple as:

@Mock
private XyzService xyzService;

@InjectMocks
private AbcController abcController;

@BeforeMethod
public void setup(){
    MockitoAnnotations.initMocks(this);
    abcController.setMyProperty("new property");
}

And that'll be enough. Going for Reflections is not advisable!

PLEASE AVOID THE USAGE OF REFLECTIONS IN PRODUCTION CODE AS MUCH AS POSSIBLE!!

For the solution of Jan Groot I must remind you that it will become very nasty since you will have to remove all the @Mock and even @InjectMocks and would have to initialize and then inject them manually which for 2 dependencies sound easy but for 7 dependencies the code becomes a nightmare (see below).

private XyzService xyzService;
private AbcController abcController;

@BeforeMethod
public void setup(){ // NIGHTMARE WHEN MORE DEPENDENCIES ARE MOCKED!
    xyzService = Mockito.mock(XyzService.class);
    abcController = new AbcController(xyzService, "new property");
}
Share:
40,709
Sam Jones
Author by

Sam Jones

Updated on July 15, 2022

Comments

  • Sam Jones
    Sam Jones almost 2 years

    I have a Spring MVC @Controller with this constructor:

    @Autowired
    public AbcController(XyzService xyzService, @Value("${my.property}") String myProperty) {/*...*/}
    

    I want to write a standalone unit test for this Controller:

    @RunWith(MockitoJUnitRunner.class)
    public class AbcControllerTest {
    
        @Mock
        private XyzService mockXyzService;
    
        private String myProperty = "my property value";
    
        @InjectMocks
        private AbcController controllerUnderTest;
    
        /* tests */
    }
    

    Is there any way to get @InjectMocks to inject my String property? I know I can't mock a String since it's immutable, but can I just inject a normal String here?

    @InjectMocks injects a null by default in this case. @Mock understandably throws an exception if I put it on myProperty. Is there another annotation I've missed that just means "inject this exact object rather than a Mock of it"?

  • Sam Jones
    Sam Jones about 8 years
    That's unfortunate. I looked through their issue tool, and it looks like they've discussed it before. General idea seems to be that it's out-of-scope for a mocking framework, and possibly even a code smell, though I don't think I agree with that. Thanks for your help!
  • Aleksandr Kravets
    Aleksandr Kravets over 2 years
    Or you could just use setter method. Any way magic disappears and at this stage even creating MyClass in @Before method is an option :(