I have a very simple rest controller:
@RestController
public class MyController {
@Autowired
public Logger logger;
The logger dependency gets injected via the following configuration:
@Configuration
public class MyConfig {
@Bean
public Logger logger() {
return LoggerFactory.getLogger(MyController.class);
}
If I run the Spring application that contains the controller then everything works fine. However, I cannot manage to achieve this dependency injection when running my unit tests. In this case I have the following test configuration:
@Configuration
@Profile("test")
public class MyTestConfig {
@Bean
public Logger logger() {
return LoggerFactory.getLogger(MyCOntroller.class);
}
And this is the relevant part of my unit tests code:
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = MyTestConfig.class)
@ActiveProfiles("test")
public class MyContollerTest {
However the logger object does not get "autowired" in MyController
(note that I do not want to mock the logger object), which results in a null pointer reference.
What am I missing?