¿Quizás alguien sabe y puede ayudarme con el simulado que se muestra a continuación? Quiero hacer un caso de prueba simulando la clase ProcessDateService. Esta por debajo en su implementación usa la Interfaz ConnectionProvider.
Quiero simular la interfaz ConnectionProvider:
@RunWith(MockitoJUnitRunner.class)
class ProcessDateServiceTest {
@Mock
private ConnectionResolver connectionResolver;
@Mock
private ConnectionProvider connectionProvider;
@Mock
private Connection connection;
@Mock
private Statement statement;
@Mock
private ResultSet resultSet;
@InjectMocks
private ProcessDateService processDateService;
@BeforeEach
void setUp() throws SQLException {
when(connectionResolver.getConnectionProvider()).thenReturn(connectionProvider);
when(connectionProvider.getConnection()).thenReturn(connection);
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(anyString())).thenReturn(resultSet);
}
@Test
void getProcessDate_Success_Test() throws SQLException {
LocalDateTime expectedDate = LocalDateTime.now();
when(resultSet.next()).thenReturn(true);
when(resultSet.getTimestamp(1)).thenReturn(java.sql.Timestamp.valueOf(expectedDate));
LocalDateTime processDate = processDateService.getProcessDate();
assertNotNull(processDate);
assertEquals(expectedDate, processDate);
}
Pero me sale el siguiente error:
java.lang.NullPointerException: Cannot invoke "com.cobis.cloud.infra.nativ.db.ConnectionResolver.getConnectionProvider()" because "this.connectionResolver" is null