0

I am having the below class

@Repository
public class ItemRepository {

    @Autowired
    DatabaseClient databaseClient;

    @Autowired
    private Environment queries;

    protected String sql(String key) {
        return queries.getProperty(key);
    }

    public Flux<ItemObj> getValues(List<String> ids, Integer year,
                                                String family, List<String> pIds,
                                                List<String> tIds) {


        return databaseClient.sql(sql("get_values"))
                .bind(ids, Parameter.fromOrEmpty(ids, String.class))
                .bind(year, Parameter.fromOrEmpty(year, Integer.class))
                .bind(family, Parameter.fromOrEmpty(family, String.class))
                .bind(pIds, Parameter.fromOrEmpty(pIds, String.class))
                .bind(tIds, Parameter.fromOrEmpty(tIds, String.class))
                .map(row -> {
                    ItemObj itemObj = new ItemObj();
                    itemObj.setItemName(row.get("item_name", String.class));
                    //rest setters from the response
                    ...
                    ...
                    ...
                    return itemObj;
                })
                .all();
    }

}

Here the get_values is defined in some properties file where the sql query is written. This implementation is working completely fine and am able to fetch data based on my query. But, I want to write unit test case for this class? Since there are some chained methods within databaseClient, I am not able to exactly mock and write test cases. Is there any good way to write test case for this class? Can anyone give example for the same?

Umang Kamdar
  • 33
  • 2
  • 6

1 Answers1

0

Database mocking such as @DataJpaTest sometimes don't do best job (in memory databases such as h2 very limited comparing to databases used for prodcution, consider testconatiners, you can use this if you are using postgresql, otherwise you can find suitable library for your db.

Sharofiddin
  • 340
  • 2
  • 14