title: Java/Spring: initMocks a class which got @Value from yml file
body: <p>I am writing unit tests on a Service class like below :</p>
  
  <pre><code>@Component
  @Profile({"default", "dev"})
  public class MyService {
      @Value("${my.property}")
      private String property;
  
      private OtherService otherService;
  
      public void MyMethod() {
          String myVar = otherService.method();
  
          return String.format("MyVar %s &amp; MyProperty %s", myVar, property);
      }
  }
  </code></pre>
  
  <p>My current test class for this test is like this:</p>
  
  <pre><code>@ActiveProfiles("dev")
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest(classes = MyApplication.class)
  public class MyServiceTest {
      @Mock
      private OtherService otherService;
  
      @InjectMocks
      private MyService myService;
  
      @BeforeEach() 
      public void init() {
          MockitoAnnotations.initMocks(this);
  
          when(otherService.method()).thenReturn("a string");
      }
  
      @Test
      public void shouldReturnException() {
          final Exception exception = assertThrows(ApiErrorException.class,
          () -&gt; myService.myMethod(var));
  
      assertThat(exception).hasMessage("here the message");
  
      verify(otherService, never()).method();
      }
  }
  </code></pre>
  
  <p>With this two classes, I have an <code>application.yml</code> &amp; <code>application-dev.yml</code> to set <code>my.property</code>.
  I want to get the property from the application-dev file during my tests execution. 
  But, with <code>@InjectMocks</code>, property is null. Whereas, using <code>@Autowired</code> in place of/with <code>@InjectMocks</code>, the property variable is set with the value present in file.</p>
  
  <p>Problem, using Autowired with/in place of InjectMock results in the <code>otherService</code> variable being initialized, so no mock is created.</p>
  
  <p>How can I still use Mockito, while having the property variable set with the value in the file?
  I saw about <code>ReflectionTestUtils.setField</code>, but using it mean having no use of a yml file (which i am not fan).</p>
  
  <p>Have a nice day</p>
  
  <hr>
  
  <p>With the help of @Deadpool, the tests can use the values written in the <code>application.yml</code> file. 
  But, using <code>@MockBean</code> and <code>@Autowired</code>, the tests get a behavior I do not understand. </p>
  
  <p>Example:
  I test that a method return an Exception, and I verify that others methods are not called after the exception was catch: <code>verify(otherService, never()).otherMethod();</code>
  Writing this line returns the following error <code>org.mockito.exceptions.verification.NeverWantedButInvoked</code>. </p>
  
  <p>The initial exception is correctly caught, but the test does not seem to acknowledge that no other services must be called. </p>