AssertionFailedError while Testing Component with Configuration via Properties

Viewed 26

Hi. I have a service and a util classes as follows:

@Service
@RequiredArgsConstructor
public class DeviceService {
    private final DeviceUtil deviceUtil;

    public String getAllDevicesDeviceType(DeviceType deviceType){
        final String appIdByDeviceType = deviceUtil.getAppIdByDeviceType(deviceType);
        // ... actually I return smth different, but for simplicity just returning appIdByDeviceType 
        return appIdByDeviceType;
    }
}

@Component
public class DeviceUtil {
    private final String androidAppId;

    public DeviceUtil(ApplicationProperties applicationProperties) {
        ApplicationProperties.DeviceClient client = applicationProperties.getDeviceClient();
        androidAppId = client.getAndroidAppId();
    }

    public String getAppIdByDeviceType(DeviceType deviceType) {
        if (deviceType == DeviceType.ANDROID) return androidAppId;
        throw InvalidInputException.of(ErrorCode.PARAMETER_INVALID, ErrorMessage.DEVICE_TYPE_INVALID);
    }

}

In ApplicationProperties class I am getting static data from application.yml file. While running project, all are ok. But when I wrote a test for DeviceUtil class as follows:

@ExtendWith(MockitoExtension.class)
class DeviceUtilTest {

    @Spy
    private ApplicationProperties applicationProperties;

    @InjectMocks
    private DeviceUtil deviceUtil;


    @Test
    void getAppIdByDeviceType_Should_ReturnSuccess() {
        String actualResult = deviceUtil.getAppIdByDeviceType(DeviceType.ANDROID);
        assertNotNull(actualResult);
        // assertThat(actualResult).isEqualTo(APP_ID);
    }
}

Test failed with foloowing description:

expected: not null
org.opentest4j.AssertionFailedError: expected: not <null>

How can I solve the issue? Thanks in advance.

0 Answers
Related