title: Spring boot component is facing nullpointer when running in unit test
body: <p>My Spring Boot app has a <code>Controller</code> that uses a <code>Service</code> that uses a bean I created (<code>SharkMaster</code>) that uses some values from a <code>properties</code> file.</p>
  
  <p>When I'm running the app in regular mode it works OK.</p>
  
  <p>However, when trying to run a unit test for the controller class, and providing it with a mock <code>service</code> it tries to create the real SharkMaster bean and fails since it can't find the values from the configuration.</p>
  
  <p>I saw <a href="https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test">several posts</a>  and read some <a href="http://www.baeldung.com/spring-testing-separate-data-source" rel="nofollow noreferrer">blog posts</a> about similar issues, but at the current stage, the app is crashing in <code>NullPointer</code> when trying to run in unit test mode.</p>
  
  <pre><code>@Configuration
  public class SharkMaster{
  
  @Value("${sharkName}")
  private String sharkName;
  
  public SharkMaster(){
    // sharkName is null here and the NullPointer is thrown
  }
  </code></pre>
  
  <p>The Controller test class:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(SharkController.class)
  @ContextConfiguration(classes = {SharkMaster.class})
  @TestPropertySource("classpath:application-test.yml")
  public class SharkControllerTest {
  
  @Autowired
  private MockMvc mockMvc;
  
  @MockBean
  private SharkService sharkService;
  </code></pre>
  
  <p>I tried to add <code>@PropertySource("classpath:application-test.yml")</code> to the Controller test class but it didn't help.</p>
  
  <p>I believe this question is not the same as <a href="https://stackoverflow.com/questions/50525053/property-values-in-bean-class-are-null-in-constructor">this one</a> , because the situation here in during the unit tests phase.</p>