2

In a Spring Boot application, I have a repository class where I am doing some DB operation with R2DBC using DatabaseClient.

My code is something like this :

return client.sql(sqlToSearch)
        .bind("alternateCodeType", alternateCodeType)
        .bind("alternateCode", alternateCode)
        .map(locationR2DBCMapper::apply).one();

where, locationR2DBCMapper is my custom BiFunction mapper class

Mapper class code is simple like this:

public class LocationR2DBCMapper implements BiFunction<Row, Object, Location> {

    @Override
    public Location apply(Row row, Object o) {
        if(row.get(0)!=null)
        {
            Location location = new Location();
            location.setId(row.get("location_id", Long.class));
            location.setLocationName(row.get("location_name", String.class));
            location.setLocationType(row.get("location_type", String.class));
            location.setProjectionMsg(row.get("projection_msg", String.class));
            location.setIsFacility(row.get("is_facility",Boolean.class));
            return location;
        }
        else {
            return new Location();
        }
    }
}

Now, I just want write a JUnit test case for this repository class, where I have to mock this DB call operation. Specifically this line:

client.sql(sqlToSearch)
        .bind("alternateCodeType", alternateCodeType)
        .bind("alternateCode", alternateCode)
        .map(locationR2DBCMapper::apply).one();

where client is object of DatabaseClient.

I tried this approach:

@BeforeEach
void setUp() {
    databaseClientMock = Mockito.mock(DatabaseClient.class);

    locationR2DBCMapperMock = Mockito.mock(LocationR2DBCMapper.class);

    DatabaseClient databaseClientMock = mock(DatabaseClient.class);
    DatabaseClient.GenericExecuteSpec executeSpecMock = mock(DatabaseClient.GenericExecuteSpec.class);

    Mono<Location> monoResultMock = mock(Mono.class);

    locationCustomRepositoryImplMock = new LocationCustomRepositoryImpl(databaseClientMock,locationR2DBCMapperMock);
}

@Test
public void getLocationByLocationTypeTest()
{
    RowsFetchSpec<?> rowsFetchSpecMock = mock(RowsFetchSpec.class);
    when(databaseClientMock.sql(anyString())).thenReturn(executeSpecMock);
    when(executeSpecMock.map(Mockito.any(BiFunction.class))).thenReturn(rowsFetchSpecMock);
    Flux<Object> resultFluxMock = Flux.empty(); // Replace this with your own Flux instance
    when(rowsFetchSpecMock.all()).thenReturn(resultFluxMock);

    //Mockito.when(databaseClientMock.sql(anyString())).thenReturn(Flux.just(Location.builder().locationName("someName").id(Long.valueOf(123)).build()));
}

but, this line giving one error, saying "Cannot resolve method 'thenReturn(Flux<Object>)'"

when(rowsFetchSpecMock.all()).thenReturn(resultFluxMock);

How can I mock this and fix the error?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sujoy
  • 25
  • 3
  • The line `Mono monoResultMock = mock(Mono.class);` does not do anything. I suggest reading [Why are my mocked methods not called when executing a unit test?](https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test), even if that might not be the immediate problem – knittl Aug 27 '23 at 08:09

0 Answers0