如何为 R2DBC DatabaseClient 的 JUnit 编写模拟类

问题描述 投票:0回答:1

在 Spring Boot 应用程序中,我有一个存储库类,我在其中使用 DatabaseClient 通过 R2DBC 执行一些数据库操作。

我的代码是这样的:

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

其中,locationR2DBCMapper 是我的自定义 BiFunction 映射器类

Mapper 类代码很简单,如下所示:

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();
        }
    }
}

现在,我只想为这个存储库类编写一个 JUnit 测试用例,我必须在其中模拟这个数据库调用操作。具体来说这一行:

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

其中 client 是 DatabaseClient 的对象。

我尝试过这种方法:

@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()));
}

但是,这一行给出了一个错误,说 “无法解析方法 'thenReturn(Flux)'”

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

我如何模拟这个并修复错误?

我刚刚嘲笑了除了 Flux 之外的所有东西。

@Mock
private lateinit var terminatingSelectOrderMock: ReactiveSelectOperation.TerminatingSelect<Entity Class>

@Mock
private lateinit var fromOrderMock: ReactiveSelectOperation.SelectWithProjection<Entity Class>

@Mock
private lateinit var selectOrderMock:ReactiveSelectOperation.ReactiveSelect<Entity Class>

@Mock
private lateinit var r2dbcTemplateFirstOld: R2dbcEntityTemplate

... ...

 Mockito.`when`(r2dbcTemplateFirstOld.select(CacheBackupEntity::class.java)).thenReturn(selectCacheMock)
Mockito.`when`(selectCacheMock.from(ArgumentMatchers.anyString())).thenReturn(fromCacheMock)
Mockito.`when`(fromCacheMock.matching(ArgumentMatchers.any())).thenReturn(terminatingSelectCacheMock)
Mockito.`when`(terminatingSelectCacheMock.all()).thenReturn(Flux.fromIterable(listOf(
   list of entities
)))

此示例适用于 kotlin。我希望,很清楚,用 java 也能做同样的事情。

java spring-webflux junit5 spring-test
1个回答
0
投票

我刚刚嘲笑了除了 Flux 之外的所有东西。

@Mock
private lateinit var terminatingSelectOrderMock: ReactiveSelectOperation.TerminatingSelect<Entity Class>

@Mock
private lateinit var fromOrderMock: ReactiveSelectOperation.SelectWithProjection<Entity Class>

@Mock
private lateinit var selectOrderMock:ReactiveSelectOperation.ReactiveSelect<Entity Class>

@Mock
private lateinit var r2dbcTemplateFirstOld: R2dbcEntityTemplate

... ...

 Mockito.`when`(r2dbcTemplateFirstOld.select(CacheBackupEntity::class.java)).thenReturn(selectCacheMock)
Mockito.`when`(selectCacheMock.from(ArgumentMatchers.anyString())).thenReturn(fromCacheMock)
Mockito.`when`(fromCacheMock.matching(ArgumentMatchers.any())).thenReturn(terminatingSelectCacheMock)
Mockito.`when`(terminatingSelectCacheMock.all()).thenReturn(Flux.fromIterable(listOf(
   list of entities
)))

此示例适用于 kotlin。我希望,很清楚,用 java 也能做同样的事情。

© www.soinside.com 2019 - 2024. All rights reserved.