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"?