I need to mock a Logger in a @Service class to make tests. I want to verify if a logger message will be created. (SLF4J Logger)
I think I can do it using @Bean class and make the Logger a managed bean on spring.
But I need to create the logger using the .class of the injected Service too.
Somebody knows how to do it?
Nathan, thanks by the duplicate tag and suggestuse appender.
My problem is i cannot use a unit test or a test without spring-context, because I have to do a integration-test with two DBs.
And the test for spring does not allow mock the logger in a first test without influences the next test.
Example:
@Service
class MigrateDataService{
@Autowired private FirstDB firstDB;
@Autowired private SecondDB secondDB;
Logger logger = LoggerFactory.getLogger(MigrateDataService.class);
@Transactional(...)
public void migrate(){
Data data firstDB.getData();
if(data.isWarn()){
logger.warn("Data {} is warning", data);
}
secondDB.save(data);
}
}
I need to detect the logger.warn call.
thanks...