vendredi 6 avril 2018

Saving and deleting data from database to test a Service

I have been working on a project in which the users will add data to the database. Once the data is sent from the android app, a service handles data storage on the server. I would like to make a test to check whether everything is being saved as it should be. Is it a good idea to make a test which uses that service to add data to DB and then delete it (since it's only used for testing).

The following is one of the methods in that service I'd like to test:

public Boolean saveProductSpecific(AddProductSpecific addProductSpecific) {
    ProductSpecific productSpecific = productSpecificRepository.save(addProductSpecific.toProductSpecific());
    /*
     * ProductSpecific wasn't saved, return false.
     */
    if (productSpecific == null)
        return false;

    ProductStore productStore = productStoreRepository
            .save(addProductSpecific.toProductStore(productSpecific.getProductSpecificId()));
    /*
     * ProductStore wasn't saved, delete previously added productSpecific and return
     * false.
     */
    if (productStore == null) {
        productSpecificRepository.delete(productSpecific);
        return false;
    }

    Price price = priceRepository.save(addProductSpecific.toPrice(productStore.getProductStoreId()));
    /*
     * Price wasn't saved, delete previously added productSpecific and productStore
     * and return false.
     */
    if (price == null) {
        productStoreRepository.delete(productStore);
        productSpecificRepository.delete(productSpecific);
        return false;
    }

    return true;
}

Aucun commentaire:

Enregistrer un commentaire