I try to Mock a Class which instantiates a DatabaseConnection via Hibernate over an other Class, but I get errors of the SLF4J Error Factory in the Class, but I want the Logger to run in this Test to. How could I fix this to Mock the class and then instantiate the List and Logger successful?
I tried to Mock the Class (Ingredientadminsitration) and then this:
PowerMockito.mockStatic(LoggerFactory.class);
when(LoggerFactory.getLogger(any(Class.class))).
thenReturn(loggerMock);
Ingredientadministration.java
@Slf4j
public class Ingredientsadministration {
private ObservableList<Ingredient> zutaten;
private SQLConnection sqlConnection;
private static Ingredientsadministration ingredientsadministration;
private Logger logger;
private Ingredientsadministration() {
logger = LoggerFactory.getLogger(this.getClass());
connectToDB();
zutaten = FXCollections.observableArrayList(sqlConnection.getZutaten());
}
public static Ingredientsadministration getInstance() {
if (ingredientsadministration == null) {
ingredientsadministration = new Ingredientsadministration();
}
return ingredientsadministration;
}
MySQLHibernate.java
@Slf4j
public class MySQLConnectHibernate implements SQLConnection {
private static SessionFactory sessionFactory;
private Session session;
private Logger logger;
private static MySQLConnectHibernate entity;
private MySQLConnectHibernate() throws ServiceException {
logger = LoggerFactory.getLogger(this.getClass());
setup();
}
public static MySQLConnectHibernate getInstance() {
if (entity == null) {
entity = new MySQLConnectHibernate();
}
return entity;
}
private void setup() throws ServiceException {
if (sessionFactory == null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
logger.debug("Create new SessionFactory={}.", sessionFactory);
}
}