How to inject real objects through InjectMocks annotation

rocky :

I have scenario where there are two properties in a class, where one property in real and other one is mock how to inject both the properties to the object.

For example.

    @RunWith(MockitoJUnitRunner.class)
    public class SampleTest extends ExchangeTestSupport {

        @InjectMocks
        private SampleTest sampleTest ;

        private SampleProperties properties;
        @Mock
        private SampleProvider provider;
}

In above code properties is real and provider is mock and need to inject both to sampleTest object.

user7294900 :

Add @Spy to inject real object

 @Spy
 private SampleProperties properties;

A field annotated with @Spy can be initialized explicitly at declaration point. Alternatively, if you don't provide the instance Mockito will try to find zero argument constructor (even private) and create an instance for you.

If you are using Spring context, also add @Autowired annotation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130453&siteId=1