0

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);
        }
    }
Coder
  • 2,153
  • 1
  • 16
  • 21
Willey3x37
  • 310
  • 2
  • 12

1 Answers1

0

I suggest you want to test if the specific message is logged? First of all if you use @sl4j you don't have to instantiate a logger by yourself. Lombok will do this for you (log.debug(..)) should be used then. You can receive the logger in your test via LoggerFactory.getLogger(SomeClass.class); You can attache a mocked appender and check if the wanted log entry was written to your mocked appender. Here's a simple example: https://dzone.com/articles/unit-testing-asserting-line

Chr3is
  • 387
  • 5
  • 21
  • Thank you but I don't really get why I have to mock the Appender of Powermock? Why it isn't working to just mock an Instance of my Class and instanciate the Logger new via Powermock then – Willey3x37 Jul 06 '19 at 17:55
  • I want that the logger works as before in the class and that the database connection isn't started – Willey3x37 Jul 07 '19 at 06:30