I am trying to call a class from another class by autowiring and it is returning a null. I have two fields autowired, one works but the other does not work.
This is the class I am trying to autowire and I am trying to get 'apppointmentAlertDays':
@Service
public class AlertsService {
public final Integer appointmentAlertDays;
public AlertsService(@Value("${alerts.appointment-alert-days}") Integer appointmentAlertDays){
this.appointmentAlertDays = appointmentAlertDays;
}
This is the class I am trying to call "AlertsService" in, however appointmentRepository does not return a null:
@Service
public class AppointmentAlertProvider implements AlertProvider {
@Autowired
private AppointmentRepository appointmentRepository;
@Autowired
private AlertsService alertsService;
@Override
public List<AppointmentEntity> findLatest(String customerId) {
Integer alertDays = alertsService.appointmentAlertDays;
}
This is where I am calling the "findLatest(String customerId)" method:
@Test
void findLatest() {
Appointment appointmentRequest = generateDummyAppointment();
Vehicle vehicle = generateDummyVehicle();
AppointmentEntity appointmentEntity = appointmentMapper.map(appointmentRequest, vehicle, null);
when(appointmentRepository.findAllByCustomerId(anyString())).thenReturn(List.of(appointmentEntity));
List<AppointmentEntity> appointmentEntities = appointmentAlertProvider.findLatest("customerId");
assertThat(appointmentEntities.size()).isOne();
}
This is where AppointmentAlertProvider is instantiated:
@Component
public class AlertProviderFactory {
private AlertProviderFactory(){}
public static AlertProvider getAlertProvider(String alertType) {
if (alertType == null || alertType.isEmpty())
return null;
if ("appointment-alert".equals(alertType)) {
return new AppointmentAlertProvider();
}
throw new IllegalArgumentException("Unknown alertType " + alertType);
}
My question is, how do I get 'appointmentAlertDays' from AlertsService? Thank you.