jeudi 21 novembre 2019

Error when test Database using Mockito in Java

I'm trying to test a database query using the Mockito framework. I don't want new records to be added to my database. I just want to test if it's possible to add these records. I read that it is possible to use Mockito. I have test function:

@Mock
Connection connection;
@Mock
Statement statement;
@Test
public void addProductTest() throws SQLException {
    try {
        connection = ConnectionManager.connectionOthers();
        when(statement = connection.createStatement()).thenReturn(statement);
        when(statement.executeUpdate("INSERT INTO produkty VALUES (NULL, 'TestowyProdukt','TestowyProducent',10,5")).thenReturn(1);
    }catch(SQLException e){
        e.printStackTrace();
    }
    finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) { /* */}
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) { /* */}
        }
    }
    assertEquals(statement,1);

}

After completing the test, I receive the following error

org.mockito.exceptions.misusing.MissingMethodInvocationException:

when() requires an argument which has to be 'a method call on a mock'.

For example: when(mock.getArticles()).thenReturn(articles);

Does anyone know what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire